Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49425
  • 博文数量: 21
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-24 15:25
文章分类

全部博文(21)

文章存档

2013年(21)

我的朋友

分类: C#/.net

2013-06-29 17:13:56

标题: C# - 简单介绍TaskScheduler
Title: C# - A Brief bump to the 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.

点击(此处)折叠或打开

  1. ///
  2.     /// This service is designed to return a TaskScheduler for application's main, UI thread.
  3.     /// This service MUST be instantiated on UI thread.
  4.     ///
  5.     [DebuggerNonUserCode]
  6.     public class UITaskSchedulerService : IUITaskSchedulerService
  7.     {
  8.         private static readonly UITaskSchedulerService InstanceField = new UITaskSchedulerService();
  9.         private static readonly TaskScheduler TaskSchedulerUI;
  10.         private static readonly Thread GuiThread;
  11.  
  12.         static UITaskSchedulerService()
  13.         {
  14.             GuiThread = Thread.CurrentThread;
  15.             TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext();
  16.         }
  17.  
  18.         ///
  19.         /// Gets the instance.
  20.         ///
  21.         public static UITaskSchedulerService Instance
  22.         {
  23.             get
  24.             {
  25.                 return InstanceField;
  26.             }
  27.         }
  28.  
  29.         ///
  30.         /// Get TaskScheduler to schedule Tasks on UI thread.
  31.         ///
  32.         /// TaskScheduler to schedule Tasks on UI thread.
  33.         public TaskScheduler GetUITaskScheduler()
  34.         {
  35.             return TaskSchedulerUI;
  36.         }
  37.  
  38.         ///
  39.         /// Check whether current tread is UI tread
  40.         ///
  41.         /// trueif current tread is UI tread.
  42.         public bool IsOnUIThread()
  43.         {
  44.             return GuiThread == Thread.CurrentThread;
  45.         }
  46.     }


 

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 定义 ,它拿到的是当前threadsynchronization context

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

点击(此处)折叠或打开

  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()
  11.                     )
  12.                 //.ContinueWith(
  13.                 // (task) => ProcessResults(task.Result),
  14.                 // TaskScheduler.FromCurrentSynchronizationContext())
  15.                 .LogTaskExceptionIfAny(Log)
  16.                 .ContinueWith(x => DataUpdater());


处理结果需要dispatchUI thread上,这样才能正确的显示。

We need to dispatch the process result back to the UI thread so that display can be properly handled.


References:


阅读(347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~