国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#

2019-11-17 03:07:19
字體:
來源:轉載
供稿:網友

C# - 簡單介紹TaskScheduler

task Scheduler根據定義

The task Scheduler by the definition blurb.

“Is the class where the usage context is within the task libraries. “

它的作用像是WPF/Winform時代的SynchronizationContext.

It is like the Synchronization context in the cross WPF/Win forms era.

像SynchronizationContext.一樣,TaskScheduler也有可能依賴特定的UI SynchronizationContext.

As with the Synchronization context, we may have requirement for like the UI context synchronization.

代碼如下:

Give the code as below.

C#代碼收藏代碼
  1. ///<summary>
  2. ///ThisserviceisdesignedtoreturnaTaskSchedulerforapplication'smain,UIthread.
  3. ///ThisserviceMUSTbeinstantiatedonUIthread.
  4. ///</summary>
  5. [DebuggerNonUserCode]
  6. publicclassUITaskSchedulerService:IUITaskSchedulerService
  7. {
  8. PRivatestaticreadonlyUITaskSchedulerServiceInstanceField=newUITaskSchedulerService();
  9. privatestaticreadonlyTaskSchedulerTaskSchedulerUI;
  10. privatestaticreadonlyThreadGuiThread;
  11. staticUITaskSchedulerService()
  12. {
  13. GuiThread=Thread.CurrentThread;
  14. TaskSchedulerUI=TaskScheduler.FromCurrentSynchronizationContext();
  15. }
  16. ///<summary>
  17. ///Getstheinstance.
  18. ///</summary>
  19. publicstaticUITaskSchedulerServiceInstance
  20. {
  21. get
  22. {
  23. returnInstanceField;
  24. }
  25. }
  26. ///<summary>
  27. ///GetTaskSchedulertoscheduleTasksonUIthread.
  28. ///</summary>
  29. ///<returns>TaskSchedulertoscheduleTasksonUIthread.</returns>
  30. publicTaskSchedulerGetUITaskScheduler()
  31. {
  32. returnTaskSchedulerUI;
  33. }
  34. ///<summary>
  35. ///CheckwhethercurrenttreadisUItread
  36. ///</summary>
  37. ///<returns><c>true</c>ifcurrenttreadisUItread.</returns>
  38. publicboolIsOnUIThread()
  39. {
  40. returnGuiThread==Thread.CurrentThread;
  41. }
  42. }

該class的要求是必須在UI thread初始化。

The requirement for the UITaskShcedulerService is that you should construct the singleton instance to start from a UI threads.

因為他內部使用的是TaskScheduler.FromCurrentSynchronizationContext,根據MSDN的TaskScheduler Class定義,它拿到的是當前thread的synchronization context

Because it internally use theTaskScheduler.FromCurrentSynchronizationContext. and from theTaskScheduler Classfrom MSDN, it retrieve the current thread’s synchronization context.

C#代碼收藏代碼
  1. Task.Factory
  2. .StartNew(
  3. ()=>
  4. _riskProvider.GetRiskPnL(),
  5. CancellationToken.None,
  6. TaskCreationOptions.None,
  7. TaskScheduler.Default)
  8. .ContinueWith(
  9. (task)=>ProcessResults(task.Result),
  10. UITaskSchedulerService.Instance.GetUITaskScheduler(
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 康平县| 罗甸县| 廊坊市| 喜德县| 台前县| 焉耆| 望都县| 高要市| 雅江县| 南郑县| 汾阳市| 临汾市| 隆昌县| 巴塘县| 定州市| 维西| 大关县| 弋阳县| 广平县| 永川市| 永年县| 新兴县| 桃园市| 榆树市| 光泽县| 泰和县| 浦城县| 乌海市| 阿拉善盟| 杭州市| 大名县| 麻城市| 华容县| 响水县| 和静县| 潍坊市| 乐山市| 靖西县| 安庆市| 黎城县| 安庆市|