Learning C # threads,

Source: Internet
Author: User

Learning C # threads,

March 17

I accidentally saw the explanation about the C # thread. After one afternoon of study, I gradually understood the thread. Here we will explain the most basic content, including thread creation, sleep, waiting, and termination.

Lab environment: Visual studio 2010.

Language used: C #

Content: creation, sleep, waiting, and thread Suspension

1. Create a new Thread object Thread t = new Thread ();

1 using System; 2 using System. threading; 3 using System. collections. generic; 4 using System. linq; 5 using System. text; 6 7 namespace thread learning 8 {9 class Program10 {11 static void PrintNumbers () 12 {13 14 Console. writeLine ("Starting... "); 15 for (int I = 1; I <10; I ++) 16 {17 Console. writeLine (I); 18} 19} 20 21 static void Main (string [] args) 22 {23 Thread t = new Thread (PrintNumbers); 24 t. start (); 25 PrintNumbers (); 26} 27} 28}
View Code

After a new object is created through Thread t = new Thread, t is a sub-Thread, t. start () starts to execute the sub-thread. The sub-thread executes the PrintNumber () function in the constructor object. The parent thread executes the PrintNumbers () function in the next row. The two threads are concurrent, so the result is not fixed.

2. The thread sleep calls the Slee () function and selects to make the thread temporarily "sleep"

Using System; using System. threading; using System. collections. generic; using System. linq; using System. text; namespace thread learning {class Program {static void PrintNumbers () {Console. writeLine ("Starting... "); for (int I = 1; I <10; I ++) {Console. writeLine (I + "parent thread") ;}} static void PrintNumbersWithDelay () {Console. writeLine ("Starting... "); for (int I = 1; I <10; I ++) {Thread. sleep (TimeSpan. fromSeconds (1); Console. writeLine (I + "sub-Thread") ;}} static void Main (string [] args) {Thread t = new Thread (PrintNumbersWithDelay); t. start (); PrintNumbers ();}}}
View Code

After the thread is created, the sub-thread t executes the PrintNumbersWithDelay () function. The sub-thread t sleeps for one second each time. The parent thread executes the PrintNumbers () function of the next row and the two threads concurrently execute the function.

3. The thread is waiting to call the join () function so that the thread can be executed in the desired order.

1 using System; 2 using System. threading; 3 using System. collections. generic; 4 using System. linq; 5 using System. text; 6 namespace thread learning 7 {8 class Program 9 {10 static void PrintNumberWithDelay () 11 {12 Console. writeLine ("Starting... "); 13 for (int I = 1; I <10; I ++) 14 {15 Thread. sleep (TimeSpan. fromSeconds (1); 16 Console. writeLine (I); 17} 18} 19 static void PrintNumbers () 20 {21 Console. writeLine ("Starting... "); 22 for (int I = 1; I <10; I ++) 23 {24 Console. writeLine (I); 25} 26} 27 static void Main (string [] args) 28 {29 Console. writeLine ("Starting... "); 30 Thread t = new Thread (PrintNumberWithDelay); 31 t. start (); 32 t. join (); 33 PrintNumbers (); 34 Console. writeLine ("Thread completed"); 35} 36} 37}
View Code

After a thread is created, the statement (that is, the parent thread) after t. join () must wait until the previous statement is executed.

4. The thread stops calling the Abort () function and kills the thread.

Using System; using System. threading; using System. collections. generic; using System. linq; using System. text; namespace thread learning {class Program {static void PrintNumbers () {Console. writeLine ("Starting... "); for (int I = 1; I <10; I ++) {Console. writeLine (I) ;}} static void PrintNumberWithDelay () {Console. writeLine ("Starting... "); for (int I = 1; I <10; I ++) {Console. writeLine (I); Thread. sleep (TimeSpan. fromSeconds (1) ;}} static void Main (string [] args) {Console. writeLine ("Starting Program .. "); Thread t = new Thread (PrintNumberWithDelay); t. start (); t. abort (); Console. writeLine ("A thread has been aborted"); t = new Thread (PrintNumbers); t. start (); PrintNumbers ();}}}
View Code

End the sub-thread with Abort. The code below is similar to the previous example.

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.