Introduction to multi-thread programming in C # [2]

Source: Internet
Author: User
This time, I will review the static methods and attributes of the programming thread involved in thread programming. Code :
 
Using system; using system. Threading; namespace threadtest {public class classsample {public void method () {While (true) {console. writeline ("mehtodd in classsample is being executed. ") ;}} Class program {public static void main (string [] ARGs) {classsample classone = new classsample (); thread threadsample = new thread (New threadstart (classone. method); threadsample. start (); While (! Threadsample. isalive) thread. sleep (1); threadsample. abort (); threadsample. join (); console. writeline (); console. writeline ("Method1 in class1 has been executed. "); Try {console. writeline (" try to re-run the class1 method thread. "); Threadsample. Start ();} catch (threadstateexception) {console. writeline (" failed to run again. "); Console. writeline () ;}console. readkey (true );}}}

notice that thread threadsample = new thread (New threadstart (classone. method); so when threadsample is started, the method in classone is executed. The specific execution process is that the static method thead is used in the while loop of the main function. sleep () sleep the main thread for a millisecond to run the threadsample thread.
then execute threadsample. the abort () method terminates the threadsample thread and calls threadsample in a try loop. the START () method wants to restart the thread. However, because the terminating thread of the abort () method cannot be recovered, an exception is thrown, Program is executed.
-------------------------------------------------------- knowledge segmentation
① first, you can see main function , all threads are dependent on the thread where the main () function is located. Main () the function is the entry of the C # program. The starting thread can be called the main thread. If all foreground threads are stopped, the main thread can be terminated, and all background threads will be terminated unconditionally. Although all threads are executed in a serial way at a micro level, you can think of them as being executed in parallel at a macro level.

② We also need to knowThreadstate attribute of Thread classThis attribute indicates the running state of the thread. Its value is
Aborted: the thread stops running;
Abortrequested: The thread. Abort () method has been called, but the thread has not stopped;
Background: The thread is executed in the background, which is related to the attribute thread. isbackground;
Running: The thread is running normally;
Stopped: the thread has been stopped;
Stoprequested: The thread is being requested to stop;
Suincluded: the thread has been suspended. (In this status, you can call resume () to run the thread again );
Suspendrequested: The thread is requesting to be suspended but cannot respond;
Unstarted: thread. Start () is not called to start the thread;
Waitsleepjoin: The thread is blocked because it calls methods such as wait (), sleep (), and join;
As mentioned above, the background State indicates that the thread is running in the background. What are the special features of the backend running threads? In fact, there is only one difference between the background thread and the foreground thread, that is, the background thread does not prevent program termination. Once all foreground threads of a process are terminated, CLR (general language runtime environment) will completely terminate the process by calling the abort () method of any surviving background process.

③ I also want to knowThread priorityWhen the CPU allocates the running time to different threads, the CPU is allocated according to the thread priority. in C #, the thread has five different priorities, from high to low, they are highest, abovenormal, normal, belownormal, and lowest. You can use the following statement to specify the thread priority:
Threadsample. Priority = threadpriority. hightest;

the following code demonstrates the attributes mentioned above:

Using system; using system. threading; namespace threadtest {public class classsample {int I; Public void methodsample () {I ++; console. writeline ("mehtodsample in classsample is being executed, and the I value is {0}", I) ;}} class program {public static void main (string [] ARGs) {classsample classone = new classsample (); thread threadone = new thread (New threadstart (classone. methodsample); threadone. start (); thread. sleep (1000); // The main thread sleeps for 1 second and runs the methodsample method console. writeline ("threadone running status: {0}", threadone. threadstate); threadone. abort (); thread threadtwo = new thread (New threadstart (classone. methodsample); // console. writeline ("threadone running status: {0}", threadone. threadstate); // If threadtwo has not been started, the threadone status should be abortrequestedthreadtwo. start (); console. writeline ("threadone running status: {0}", threadone. threadstate); console. writeline ("threadtwo priority: {0}", threadtwo. priority); threadtwo. priority = threadpriority. highest; console. writeline ("threadtwo priority: {0}", threadtwo. priority); console. readkey (true );}}}

The execution result is as follows:
 

To be continued, we will review the use of the timer in multi-thread programming.

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.