标题: 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.
/// <summary> /// This service is designed to return a TaskScheduler for application's main, UI thread. /// This service MUST be instantiated on UI thread. /// </summary> [DebuggerNonUserCode] public class UITaskSchedulerService : IUITaskSchedulerService { private static readonly UITaskSchedulerService InstanceField = new UITaskSchedulerService(); private static readonly TaskScheduler TaskSchedulerUI; private static readonly Thread GuiThread; static UITaskSchedulerService() { GuiThread = Thread.CurrentThread; TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext(); } /// <summary> /// Gets the instance. /// </summary> public static UITaskSchedulerService Instance { get { return InstanceField; } } /// <summary> /// Get TaskScheduler to schedule Tasks on UI thread. /// </summary> /// <returns>TaskScheduler to schedule Tasks on UI thread.</returns> public TaskScheduler GetUITaskScheduler() { return TaskSchedulerUI; } /// <summary> /// Check whether current tread is UI tread /// </summary> /// <returns><c>true</c>if current tread is UI tread.</returns> public bool IsOnUIThread() { return GuiThread == Thread.CurrentThread; } }
该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 the TaskScheduler.FromCurrentSynchronizationContext. and from the TaskScheduler Class from MSDN, it retrieve the current thread’s synchronization context.
Task.Factory .StartNew( () => _riskProvider.GetRiskPnL(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default) .ContinueWith( (task) => ProcessResults(task.Result), UITaskSchedulerService.Instance.GetUITaskScheduler() ) //.ContinueWith( // (task) => ProcessResults(task.Result), // TaskScheduler.FromCurrentSynchronizationContext()) .LogTaskExceptionIfAny(Log) .ContinueWith(x => DataUpdater());
处理结果需要dispatch到UI thread上,这样才能正确的显示。
We need to dispatch the process result back to the UI thread so that display can be properly handled.
References:
相关推荐
C#利用Interop.TaskScheduler.dll添加删除计划任务,可实现程序随Windows系统自动启动; 项目用VS2017打开,需要.net 2.0支持,需要管理员权限;支持win7 win10;不支持xp。
Laravel开发-laravel-task-scheduler 轻量级的Laravel任务调度包
apache-dolphinscheduler-1.3.2-dolphinscheduler-bin.tar.gz
经过测试可以适用solr7.4版本。如果低版本solr(6.*) 可以直接适用网上的solr-dataimport-scheduler 1.1 或者1.0版本。
Arduino-TaskScheduler.zip,Arduino、ESPX和STM32微控制器任务调度器的协同多任务处理,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的欢迎,Arduino通过开源系统提供了很多灵活性...
solr定时索引使用到的定时调度器jar包, 可使用于solr7.x版本
3-3.新一代大数据调度+-Apache+DolphinScheduler架构演进+&+Roadmap.pdf
ds.1.2.0前台稳定版本。
ds分布式工作流稳定版本后台
solr-data-import-scheduler-1.1.2,用于solr定时更新索引的jar包,下载后引入到solr本身的dist下面,或者你tomcat项目下面的lib下面
TaskSchedulerTaskScheduler,它决定了task该如何被调度,而在.net framework中有两种系统定义Scheduler,第一个是Task默认的ThreadPoolTaskScheduler,还是一种SynchronizationContextTaskScheduler,以及这两种...
兼容solr6.5.1 定时任务依赖jar包
solr 增量更新所需要的包 solr-dataimporthandler-6.5.1 + solr-dataimporthandler-extras-6.5.1 + solr-data-import-scheduler-1.1.2
lts分布式定时任务源码
apache-dolphinscheduler-3.1.4-bin.tar.gzapache-dolphinscheduler-3.1.4-bin.tar.gzapache-dolphinscheduler-3.1.4-bin.tar.gzapache-dolphinscheduler-3.1.4-bin.tar.gzapache-dolphinscheduler-3.1.4-bin.tar....
hadoop-0.21.0-fairscheduler.jar
light-task-scheduler-master.zip
apache-dolphinscheduler-3.1.4-src.tar.gzapache-dolphinscheduler-3.1.4-src.tar.gzapache-dolphinscheduler-3.1.4-src.tar.gzapache-dolphinscheduler-3.1.4-src.tar.gzapache-dolphinscheduler-3.1.4-src.tar....
使用编程方式添加计划任务的.net组件taskscheduler