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.
- /// <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# - 简单介绍TaskScheduler的更多相关文章
- [原创]关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...
- yii2的权限管理系统RBAC简单介绍
这里有几个概念 权限: 指用户是否可以执行哪些操作,如:编辑.发布.查看回帖 角色 比如:VIP用户组, 高级会员组,中级会员组,初级会员组 VIP用户组:发帖.回帖.删帖.浏览权限 高级会员组:发帖 ...
- angular1.x的简单介绍(二)
首先还是要强调一下DI,DI(Denpendency Injection)伸手获得,主要解决模块间的耦合关系.那么模块是又什么组成的呢?在我看来,模块的最小单位是类,多个类的组合就是模块.关于在根模块 ...
- Linux的简单介绍和常用命令的介绍
Linux的简单介绍和常用命令的介绍 本说明以Ubuntu系统为例 Ubuntu系统的安装自行百度,或者参考http://www.cnblogs.com/CoderJYF/p/6091068.html ...
- iOS-iOS开发简单介绍
概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...
- iOS开发多线程篇—多线程简单介绍
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
随机推荐
- python添加tab键自动补全功能
默认python是没有tab键补全功能的: >>> import tab Traceback (most recent call last): File "<stdi ...
- activity的onCreate参数 saveInstanceState
写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是 ...
- php_memcache 缓存 下载-安装-配置-学习
一.安装php_memcache.dll 打开phpinfo() 查看PHP Extension Build,如TS,VC11 查看Architecture,如X86.X64 查看PHP版本,如5.6 ...
- 如何设置Docker容器中Java应用的内存限制
如果使用官方的Java镜像,或者基于Java镜像构建的Docker镜像,都可以通过传递 JAVA_OPTS 环境变量来轻松地设置JVM的内存参数.比如,对于官方Tomcat 镜像,我们可以执行下面命令 ...
- 《Effective Java》读书笔记四(泛型)
Java1.5发行版本中增加了泛型(Generic).在没有泛型之前,从集合中读取到的每一个对象都必须进行转换.如果有人不小心插入了错误的类型对象,在运行时的转换处理就会出错.有了泛型之后,可以告诉编 ...
- 使用UINavigationController后导致UIScollView尺寸变化
转自:http://www.w3c.com.cn/%E4%BD%BF%E7%94%A8uinavigationcontroller%E5%90%8E%E5%AF%BC%E8%87%B4uiscollv ...
- The DiskSpd Storage Performance Tool
https://enterpriseitnotes.wordpress.com/2013/05/31/understanding-ios-iops-and-outstanding-ios/ https ...
- 针对Properties中实时性要求不高的配置参数,用Java缓存起来
Properties常用于项目中参数的配置,当项目中某段程序需要获取动态参数时,就从Properties中读取该参数,使程序是可配置的.灵活的. 有些配置参数要求立即生效,有些则未必: 一.实时性要求 ...
- 20个angularjs开发工具
AngularJS是那些渴望以动态方式来设计web app的web开发人员最偏爱的框架之一.如果你是一个希望启动AngularJS项目的开发人员,那么你可能需要帮助来挑选出趁手的工具…… 在Value ...
- oracle中的字符串函数详解
花了点时间 复习.了一下字符串函数 希望对初学者有帮助 ----------连接字符串函数-----------------select concat('leiyi','hubei') from du ...