在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.定義一個線程 var task1 = Task.Factory.StartNew(() => DoSomeWork()); 方法如下:
View Code
private static object DoSomeWork() 2.多線程執行后,執行另一個方法 var task1 = Task.Factory.StartNew(() => DoSomeWork()).ContinueWith( 3.等待所有線程結束
var task1 = Task.Factory.StartNew(() => DoSomeWork()); var task2 = Task.Factory.StartNew(() => DoSomeWork()); var task3 = Task.Factory.StartNew(() => DoSomeWork()); Task.WaitAll(task1, task2, task3); 4.等待其中一個線程結束
var task1 = Task.Factory.StartNew(() => DoSomeWork()); var task2 = Task.Factory.StartNew(() => DoSomeWork()); var task3 = Task.Factory.StartNew(() => DoSomeWork()); Task.WaitAny(task1, task2, task3); 5.等待所有線程結束執行的方法
var task1 = Task.Factory.StartNew(() => { Thread.Sleep(3000);
return "dummy value 1" ;
}); var task2 = Task.Factory.StartNew(() => { Thread.Sleep(3000);
return "dummy value 2" ;
}); var task3 = Task.Factory.StartNew(() => { Thread.Sleep(3000);
return "dummy value 3" ;
}); Task.Factory.ContinueWhenAll( new [] { task1, task2, task3 }, tasks =>
{ foreach (Task< string > task in tasks)
{
Console.WriteLine(task.Result);
}
}); |
请发表评论