Write in front: Reading this series of articles means that you already know what a thread is, and now you want to know how to use threading correctly for programming.
<summary>///single-threaded working example//</summary>private static void Singlethreaddemo () {Console.WriteLine ("Main Thread "); Thread thread = new Thread (() =>{for (var i = 0; i < ten; i++) {Console.WriteLine (String.Format ("Thread out:{0}", i)); Thread.Sleep (500);} Console.WriteLine ("Sub Thread the End");}); Thread. Start (); Console.WriteLine ("Main thread Wait for Sub Thread");} <summary>///Multiple Threads Working example//</summary>private static void Multithreaddemo () {ThreadStart task = () =>{for (var i = 0; i < i++) {Console.WriteLine (String.Format ("Thread {0} out:{1}", Thread.CurrentThread.Name, i)); Thread.Sleep (500);} Console.WriteLine (String.Format ("Sub Thread {0} The End", Thread.CurrentThread.Name);}; Console.WriteLine ("Main Thread"); Thread thread1 = new Thread (Task) {Name = "thread1"};thread1. Start (); Thread thread2 = new Thread (Task) {Name = "thread2"};thread2. Start (); Thread thread3 = new Thread (Task) {Name = "thread3"};thread3. Start (); Console.WriteLine ("Main Thread Wait for Sub ThrEAD ");} static void Main (string[] args) {//singlethreaddemo ();//multithreaddemo (); Console.ReadLine ();}
C # Multi-Threading Usage 1-simple example