1,异步委托方式
1 static void Main(string[] args) 2 { 3 //异步委托开启线程 4 Action<int, int, int> a = Add; 5 a.BeginInvoke(1, 2, 3, null, null); 6 7 } 8 static void Add(int a, int b, int c) 9 { 10 Console.WriteLine(a + b + c); 11 }
2,Thread类开启线程方式
static void Main(string[] args) { Thread thread = new Thread(PrintJpeg); thread.Start("D:/1.jpg"); } static void PrintJpeg(object FilePath) { Console.WriteLine("打印" + FilePath.ToString() + "路径下的图片..."); Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId); }
3,通过任务开启线程方式
static void Main(string[] args) { Task t = new Task(PrintPDF);//方式一,task开启线程 t.Start(); //task开启线程的lambda书写方式 Task.Run(() => { PrintPDF(); }); TaskFactory tf = new TaskFactory();//方式二,TaskFactory开启线程 tf.StartNew(PrintPDF); //TaskFactory开启线程的lambda书写方式 tf.StartNew(() => { PrintPDF(); }); }