.Net4.0并行库介绍——Cancellation Framework
在.net 4.0中,引入了一個新的類CancellationToken,這個類基本上集成了我們各種常用的取消方式,在并發任務中非常有用。
同步模式下的取消:
一種比較常見的需要支持取消功能的的是一些比較耗時的分段操作:如視頻轉換,網絡下載等,這種方式下的取消機制如下:
使用方式如下:
????EventHandler externalEvent; ????void Example1() ????{ ????????CancellationTokenSource cts = new CancellationTokenSource(); ????????externalEvent += ?????????? (sender, obj) => { cts.Cancel(); }; //wire up an external requester ????????try ????????{ ????????????int val = LongRunningFunc(cts.Token); ????????} ????????catch (OperationCanceledException) ????????{ ????????????//cleanup after cancellation if required... ????????} ????}
????private static int LongRunningFunc(CancellationToken token) ????{ ????????int total = 0; ????????for (int i = 0; i < 1000; i++) ????????{ ????????????for (int j = 0; j < 1000; j++) ????????????{ ????????????????total++; ????????????} ????????????if (token.IsCancellationRequested) ????????????{ // observe cancellation ????????????????throw new OperationCanceledException(token); // acknowledge cancellation ????????????} ????????} ????????return total; ????}
異步模式下的取消
另外一種常見的方式是在一些異步操作中,往往不能主動釋放,只能等待異步操作回調的時候才能操作結果。此時一般取消方法如下:
使用方式如下:
????void BlockingOperation(CancellationToken token) ????{ ????????ManualResetEvent mre = new ManualResetEvent(false); ????????//register a callback that will set the MRE ????????CancellationTokenRegistration registration = ?????????? token.Register(() => mre.Set()); ????????using (registration) ????????{ ????????????mre.WaitOne(); ????????????if (token.IsCancellationRequested) //did cancellation wake us? ????????????????throw new OperationCanceledException(token); ????????} //dispose the registration, which performs the deregisteration. ????}
這里我們通過CancellationToken注冊了一個回調方法以通知任務等待線程,也可以以我們經常使用的WaitHandle的那樣的方式使用。
????void Wait(WaitHandle wh, CancellationToken token) ????{ ????????WaitHandle.WaitAny(new[] { wh, token.WaitHandle }); ????????if (token.IsCancellationRequested) //did cancellation wake us? ????????????throw new OperationCanceledException(token); ????}
高級應用
由于例子比較簡單,這里就只列舉一下代碼,不多介紹了。
一個CancellationToken對應多個任務
????void Example4() ????{ ????????CancellationTokenSource cts = new CancellationTokenSource(); ????????Func1(cts.Token); ????????Func2(cts.Token); ????????Func3(cts.Token); ????????//... ????????cts.Cancel(); // all listeners see the same cancellation request. ????}
一個任務對應多個CancellationToken
????void LinkingExample(CancellationToken ct1, CancellationToken ct2) ????{ ????????CancellationTokenSource linkedCTS = ????????CancellationTokenSource.CreateLinkedTokenSource(ct1, ct2); ????????try ????????{ ????????????SlowFunc(linkedCTS.Token); ????????} ????????catch (OperationCanceledException oce) ????????{ ????????????if (ct1.IsCancellationRequested) ????????????{ ????????????????// ... ????????????} ????????????else if (ct2.IsCancellationRequested) ????????????{ ????????????????// ... ????????????} ????????} ????????linkedCTS.Dispose(); // clean up the linking. required. ????}
最后我們再來一個并發查詢時取消的例子:
????private void RunQuery() ????{ ????????int[] data = { 1, 2, 3 }; ????????CancellationTokenSource cts = new CancellationTokenSource(); ????????var query = data.AsParallel() ???????????????????? .WithCancellation(cts.Token) // token given to library code ???????????????????? .Select((x) => SlowFunc(x, cts.Token)); // token passed to user code ????}
????private int SlowFunc(int x, CancellationToken token) ????{ ?????? int result ?????? while(...) ?????? { ????????? if (token.IsCancellationRequested) ???????????? throw new OperationCanceledException(token); ????????? ... ?????? } ?????? return result; ????}
小結
.net 4.0中的Cancellation Framework還是非常實用的,通過它可以更有效的簡化及規范的使用各種取消的操作方式,由于我也只會皮毛,在這里也只是介紹了它的基本用法,在后續的學習和應用中將繼續進一步介紹。
轉載于:https://www.cnblogs.com/zjoch/p/3508301.html
總結
以上是生活随笔為你收集整理的.Net4.0并行库介绍——Cancellation Framework的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript ||用法
- 下一篇: IOS开发之--UIScrollView