Application of multithreading in Visual C # network programming

Source: Internet
Author: User
Tags constructor thread thread class

Network applications are more or less used to threading, or even a slightly more powerful network application will always be in a more or less thread, if the application in the number of threads out of more than two, then you can call this program multithreaded applications. So why are web apps always intertwined with threads? This is because the network application in the implementation of the time, will encounter many unexpected problems, the most common is network congestion and network waiting.

Programs in dealing with these problems often need to spend a lot of time, if not using the thread, then the program will perform in the performance of such as slow, long execution time, prone to errors, unresponsive and so on. And if these processes that can cause a large amount of program execution time to be handled in a thread, they tend to greatly improve the operational efficiency and performance of the application and achieve better scalability. So does this mean that threads should be used extensively in Web applications? This is not the case, the thread is actually a double-edged sword, if not the occasion, where no need to use the use of force may generate a lot of program garbage, or at the end of the program, because there is no ability to destroy the process created to cause the application hangs and so on.

So if you think you're writing code fast enough, I'm going to give you the advice of not using threads or multithreading. Here's a reminder that if you're not quite sure about the threads and the principles and mechanisms behind windows, you can refer to the books on Windows operating systems, which are generally described in more detail. Then read this article again.

I. Introduction creating and Using Threads in Visual C #:

The threads used in Visual C # are all often instantiated through the thread class in the System.Threading of the namespace. The thread class's constructor is used to create threads that are available to Visual C #, setting the thread properties and controlling the state of the thread through the methods and properties in thread. The most typical constructor syntax in the following thread class is typically used in Visual C # to create and initialize the thread instance.

public Thread (
 ThreadStart start
) ;

Parameters

The start ThreadStart delegate, which references the method to invoke when this thread starts execution.

Thread also provides additional constructors to create threads, which are not covered here. Table 01 is some of the commonly used methods in the thread class and their brief descriptions:

/tr>
method description
interrupt
join
resetabort
resume
sleep current thread blocking specified number of milliseconds 。
The operating system changes the state of the current instance to threadstate.running.
suspend > Hang the thread, or if the line Process is suspended, it does not work.
Common methods of table 01:thread classes and their descriptions

Here's what you should be aware of. NET executes a thread, which is usually automatically destroyed when the thread finishes execution. If the thread is not automatically destroyed, it can be manually destroyed by the Abort method in thread, but also note that if the resource used in the thread is not completely destroyed, the Abort method does not guarantee that the thread will be destroyed after it is executed. Some properties are also provided in the thread class to set and get the thread instance properties created, and some common properties of the thread class and their descriptions are described in table 02:

/tr>
description
currentthread
isalive
isbackground Get or set A value that indicates whether a thread is a background thread.
name
priority
Gets a value that contains the state of the current thread.
Table 02:thread Common properties of classes and their descriptions

Two. The main content of this article and program debugging and operating environment:

The main content of this article is to introduce the more high-performance improvements that multithreading brings to writing network applications in Visual C #. The specific approach is to use two different methods in Visual C #, one with multiple threads and the other not to implement the same example of a specific network application, which is the function of obtaining the online status and the corresponding computer name of the computer that corresponds to multiple IP addresses on the same network segment of the network. By comparing the different execution efficiencies of these two methods, we can know how important multithreading is to improve the execution efficiency of the network application. The following are the basic environment configurations designed to debug and run programs in this article:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2002 Official edition,. NET FrameWork SDK version number 3705.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.