c sharp multithreading

1.  静态方法

 1 using System;
 2 using System.Threading;
 3 
 4 namespace PlusThread
 5 {
 6     class Program
 7     { 
 8         static void Main(string[] args)
 9          {
10             //创建无参的线程
11             //Thread thread1 = new Thread(new ThreadStart(Thread1));
12             Thread thread1 = new Thread( (Thread1));
13             thread1.Start();
14              Console.ReadLine();
15          }
16  
17          static void Thread1()
18           {
19               Console.WriteLine("这是无参的方法");
20           }
21       }
22 }
View Code

2.实例方法

 1 using System;
 2 using System.Threading;
 3 
 4 namespace PlusThread
 5 {
 6     class Program
 7     { 
 8         static void Main(string[] args)
 9         {
10             testThread test = new testThread();
11             Thread t1 = new Thread(new ThreadStart(test.fun)); 
12             t1.Start();
13             
14             Console.ReadLine();
15          }
16        
17     }
18 
19     class testThread
20     {
21         public void fun()
22         {
23             Console.WriteLine("这是实例方法");
24         }
25     }
26 
27 }
View Code

简洁写法:

 1 using System;
 2 using System.Threading;
 3 
 4 namespace PlusThread
 5 {
 6     class Program
 7     { 
 8         static void Main(string[] args)
 9         {
10              
11             Thread t1 = new Thread(delegate() { Console.WriteLine("匿名委托创建线程"); });
12             Thread t2 = new Thread(()=> { Console.WriteLine("lambda创建线程"); Console.WriteLine("hello"); });
13             t1.Start();
14             t2.Start();
15             Console.ReadLine();
16          }       
17     }
18 }
View Code

3.

猜你喜欢

转载自www.cnblogs.com/https/p/9345394.html