C#开启异步 线程的四种方式(二)

一、异步委托开启线程

public static void Main(string[] args){

Action<int,int> a=add;
a.BeginInvoke(3,4,null,null);//前两个是add方法的参数,后两个可以为空
Console.WriteLine("main()");
Console.ReadKey();


static void add(int a,int b)
Console.WriteLine(a+b);


static void add(int a,int b){
Console.WriteLine(a+b);

二 、使用threadPool

ThreadPool.QueueUserWorkItem("方法名");
ThreadPool.QueueUserWorkItem("方法名");
ThreadPool.QueueUserWorkItem("方法名");
ThreadPool.QueueUserWorkItem("方法名"); //带有参数object

三 、 使用Task new的方式

Task task = new Task(()=> Console.WriteLine("开启任务异步多线程3") );

四、使用Task Factory的方式

  Task task1 = Task.Factory.StartNew(() =>  Console.WriteLine("开启任务异步多线程4"));

转自:https://www.cnblogs.com/dashanboke/p/10650502.html

在通往幸福道路上,并没有什么捷径可走,唯有付出努力和拼搏
原文地址:https://www.cnblogs.com/xinhuawei/p/15570777.html