.Net中Task Parallel Library的基本用法
我們知道,每個應(yīng)用程序就是一個進程,一個進程有多個線程。Task Parallel Library為我們的異步編程、多線程編程提供了強有力的支持,它允許一個主線程運行的同時,另外的一些線程或Task也同時運行。本篇體驗基本用法。
基本用法
Taks的構(gòu)造函數(shù)接收的類型是Action,也就是一個委托。
static void Main(string[] args) { var t1 = new Task(() => { Console.WriteLine("任務(wù)1開始"); Thread.Sleep(1000); Console.WriteLine("任務(wù)1結(jié)束"); }); t1.Start(); Console.ReadKey(); }
如果把方法放到外面。
static void Main(string[] args) { var t1 = new Task(() => DoSth(1,2000)); t1.Start(); Console.ReadKey(); } static void DoSth(int id, int sleepTime) { Console.WriteLine("任務(wù){(diào)0}開始",id); Thread.Sleep(sleepTime); Console.WriteLine("任務(wù){(diào)0}結(jié)束",id); }
如果有多個Task同時執(zhí)行。
static void Main(string[] args) { var t1 = new Task(() => DoSth(1,2000)); t1.Start(); var t2 = new Task(() => DoSth(2, 1500)); t2.Start(); var t3 = new Task(() => DoSth(3, 3000)); t3.Start(); Console.ReadKey(); }
如果有很多Task,每個Task手動啟動的話很費事,Task Parallel Library為我們準(zhǔn)備了Task工廠。
static void Main(string[] args) { var t1 = Task.Factory.StartNew(() => DoSth(1, 2000)); var t2 = Task.Factory.StartNew(() => DoSth(2, 1500)); var t3 = Task.Factory.StartNew(() => DoSth(3, 3000)); Console.ReadKey(); }
如果我們想在一個任務(wù)結(jié)束之后立即執(zhí)行某個任務(wù),可以使用ContinueWith方法。
static void Main(string[] args) { var t1 = Task.Factory.StartNew(() => DoSth(1, 2000)).ContinueWith((pre)=> DoOtherThing(4,2000)); var t2 = Task.Factory.StartNew(() => DoSth(2, 1500)); var t3 = Task.Factory.StartNew(() => DoSth(3, 3000)); Console.ReadKey(); } static void DoSth(int id, int sleepTime) { Console.WriteLine("任務(wù){(diào)0}開始",id); Thread.Sleep(sleepTime); Console.WriteLine("任務(wù){(diào)0}結(jié)束",id); } static void DoOtherThing(int id, int sleepTime) { Console.WriteLine("其他任務(wù){(diào)0}開始", id); Thread.Sleep(sleepTime); Console.WriteLine("其他任務(wù){(diào)0}結(jié)束", id); }
如果希望等待所有的Task執(zhí)行完畢,使用WaitAll方法。
static void Main(string[] args) { var t1 = Task.Factory.StartNew(() => DoSth(1, 2000)); var t2 = Task.Factory.StartNew(() => DoSth(2, 1500)); var t3 = Task.Factory.StartNew(() => DoSth(3, 3000)); var taskList = new List<Task> {t1, t2, t3}; Task.WaitAll(taskList.ToArray()); Console.WriteLine("我是在所有Task執(zhí)行完畢后才執(zhí)行的"); Console.ReadKey(); }
如果想手動取消結(jié)束某個Task,需要為方法帶上CancellationToken類型參數(shù)。
static void Main(string[] args) { var source = new CancellationTokenSource(); try { var t1 = Task.Factory.StartNew(() => DoSth(1, 1000, source.Token)) .ContinueWith((pre) => DoOtherThing(2, 2000)); source.Cancel(); } catch (Exception ex) { Console.WriteLine(ex.GetType()); } Console.WriteLine("haha"); Console.ReadKey(); } static void DoSth(int id, int sleepTime, CancellationToken token) { if (token.IsCancellationRequested) { Console.WriteLine("任務(wù)被取消"); token.ThrowIfCancellationRequested(); } Console.WriteLine("任務(wù){(diào)0}開始",id); Thread.Sleep(sleepTime); Console.WriteLine("任務(wù){(diào)0}結(jié)束",id); } static void DoOtherThing(int id, int sleepTime) { Console.WriteLine("其他任務(wù){(diào)0}開始", id); Thread.Sleep(sleepTime); Console.WriteLine("其他任務(wù){(diào)0}結(jié)束", id); }
如何從Task從獲取方法的返回結(jié)果呢?
static void Main(string[] args) { Console.WriteLine("開始計算"); Task<int> t = Task.Factory.StartNew(() => Sum(1, 2)); Console.WriteLine("等待結(jié)果"); Console.WriteLine(t.Result); Console.ReadKey(); } static int Sum(int a, int b) { return a + b; }
后面一個Task獲取前面一個Task的返回值。
static void Main(string[] args) { Task<string> firstTask = Task.Factory.StartNew<string>(() => { Console.WriteLine("第一個任務(wù)開始"); return "hi from the one"; }); Task secondTask = firstTask.ContinueWith((prevoursTask) => { Console.WriteLine("這里是第二個任務(wù),獲取到第一個任務(wù)的返回值是{0}",prevoursTask.Result,TaskContinuationOptions.OnlyOnRanToCompletion); }); secondTask.Wait(); Console.ReadKey(); }
等待所有Task完成。
static void Main(string[] args) { var t1 = Task.Factory.StartNew(() => { Console.WriteLine("第一個任務(wù)"); Thread.Sleep(1000); }); var t2 = Task.Factory.StartNew(() => { Console.WriteLine("第二個任務(wù)"); Thread.Sleep(1000); }); var taskList = new List<Task> {t1, t2}; Task.Factory.ContinueWhenAll(taskList.ToArray(), (t) => { Console.WriteLine("所有任務(wù)完成我就出來"); }); Console.ReadKey(); }
如果是嵌套Task。
static void Main(string[] args) { Task.Factory.StartNew(() => { Task child = Task.Factory.StartNew(() => { Console.WriteLine("我是子任務(wù)"); }, TaskCreationOptions.AttachedToParent); }).Wait(); Console.ReadKey(); }
啟動Task的幾種方式
1、通過Task.Factory.StartNew方法。
static void Main(string[] args) { Task.Factory.StartNew(() => SaySth("hello")); Console.ReadKey(); } static void SaySth(string msg) { Console.WriteLine(msg); }
2、通過Task的Start實例方法
static void Main(string[] args) { var t = new Task(() => SaySth("hello")); t.Start(); Console.ReadKey(); }
或者干脆用委托。
static void Main(string[] args) { Task t = new Task(delegate {SaySth("hello");}); t.Start(); Console.ReadKey(); }
3、Task的靜態(tài)方法Run
static void Main(string[] args) { Task t = Task.Run(() => SaySth("hello")); Console.ReadKey(); } static void SaySth(string msg) { Console.WriteLine(msg); }
一個例子
比如說要下載某個頁面,在保持當(dāng)前UI界面無影響的情況下,使用Task在后臺啟動任務(wù)下載某個頁面。
static void Main(string[] args) { Console.WriteLine("界面內(nèi)容"); Task<string> r = DownloadAsync("http://www.baidu.com"); while (!r.IsCompleted) { Console.Write("."); Thread.Sleep(250); } Console.WriteLine(r.Result); Console.ReadKey(); } private static string DownloadWebPage(string url) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); var reader = new StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } private static Task<string> DownloadAsync(string url) { return Task.Factory.StartNew(() => DownloadWebPage(url)); }
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
ASP.NET 5已終結(jié),迎來ASP.NET Core 1.0和.NET Core 1.0
命名是非常困難的事情,微軟這次為了和ASP.NET4.6做區(qū)分,采用了全新的命名方式ASP.NET Core 1.0,它是一個全新的框架。2016-03-03那些年,我還在學(xué)asp.net(一) 學(xué)習(xí)筆記
那些年到此,基本學(xué)習(xí)了前端的基本知識,那些年的第四課就是asp.net,當(dāng)然那時看了很多教程,比如說:天轟穿,當(dāng)然天轟穿說得比較多,如面向?qū)ο?,C#知識,由于當(dāng)時上過C++,所以就沒有看這些,直接從asp.net開始,主要是學(xué)習(xí)一下asp.net用到的一些基本控件2012-03-03.Net創(chuàng)建型設(shè)計模式之原型模式(Prototype)
這篇文章介紹了.Net設(shè)計模式之原型模式(Prototype),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05Entity Framework系統(tǒng)架構(gòu)與原理介紹
這篇文章介紹了Entity Framework系統(tǒng)架構(gòu)與原理,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03