Multi-thread programming in. Net

Source: Internet
Author: User

Multi-thread programming in. Net
Multithreading is a feature of many operating systems. It can greatly improve the running efficiency of programs. Therefore, multithreading programming technology is widely used by programmers. At present, Microsoft's. Net strategy is being further promoted, and various related technologies are accepted by the majority of programmers. Also, multi-threaded programming technology in. Net has a very important position. This article will introduce you to the basic methods and steps for multi-threaded programming under. Net.



Start a new thread



It is very easy to create a new thread under. Net. You can use the following statements to start a new thread:


Thread thread = new Thread (new ThreadStart (ThreadFunc ));

Thread. Start ();



The first statement creates a new Thread object and specifies a method for this Thread. When a new thread starts, this method is called and executed. This thread object calls the thread method to be called through an instance of the System... Threading. ThreadStart class using a type-safe method.


The second statement officially starts the new thread. Once the method Start () is called, the thread remains in the "alive" state, you can read its IsAlive attribute to determine whether it is in the "alive" status. The following statement shows how to suspend a thread if it is in the "alive" state:


If (thread. IsAlive ){

Thread. Suspend ();

}



However, note that the Start () method of the thread object only starts the thread, and does not guarantee that the thread method ThreadFunc () can be executed immediately. It only ensures that the thread object can be allocated to the CPU time, and the actual execution is determined by the operating system based on the processor time.


A thread's method does not contain any parameters, nor returns any value. Its naming rules are the same as those for general functions. It can be either static or non-static ). After it is executed, the corresponding thread ends, and the IsAlive attribute of Its thread object is set to false. The following is an example of a thread method:


Public static void ThreadFunc ()

{

For (int I = 0; I <10; I ++ ){

Console. WriteLine ("ThreadFunc {0}", I );

}

}




Foreground thread and background thread


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.