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.
![](http://joe-bq-wang.iteye.com/images/icon_star.png)
- /// <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.
![](http://joe-bq-wang.iteye.com/images/icon_star.png)
- 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:
TaskScheduler Class
转载于:https://www.cnblogs.com/xiaochao12345/p/3761490.html
C# - 简单介绍TaskScheduler相关推荐
- 遗传算法的简单介绍以及模式定理的简单证明
遗传算法 遗传算法(Genetic Algorithm,GA),最早是由美国的John holland在20世纪70年代提出.算法通过模拟达尔文生物进化论的自然选择以及遗传学机理的生物进化过程来搜 ...
- 2021年大数据ELK(十八):Beats 简单介绍和FileBeat工作原理
全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 Beats 简单介绍和FileBeat工作原理 一.Beats 二.FileB ...
- 2021年大数据ELK(十五):Elasticsearch SQL简单介绍
全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 Elasticsearch SQL简单介绍 一.SQL与Elasticsear ...
- 2021年大数据ELK(二):Elasticsearch简单介绍
全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 一.Elasticsearch简介 1.介绍 2.创始人 二.E ...
- iOS开发UI篇—多控制器和导航控制器简单介绍
iOS开发UI篇-多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...
- 简单介绍一下R中的几种统计分布及常用模型
统计学上分布有很多,在R中基本都有描述.因能力有限,我们就挑选几个常用的.比较重要的简单介绍一下每种分布的定义,公式,以及在R中的展示. 统计分布每一种分布有四个函数:d――density(密度函数) ...
- LVS(Linux Virtual Server)三种负载均衡模型和十种调度的简单介绍
LVS(Linux Virtual Server)三种负载均衡模型和十种调度的简单介绍 LVS (Linux Virtual Server) LVS(Linux Virtual Server)其实就是 ...
- dubbo学习过程、使用经验分享及实现原理简单介绍
一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...
- iOS开发UI篇—UIWindow简单介绍
iOS开发UI篇-UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...
最新文章
- libevent中的hash表
- java两个日期之间的日期_获取两个日期之间的日期形成一个集合
- MongoDB安装与副本集配置
- oracle+测试权限,Oracle测试题
- 视频通信关键技术探索及实践
- 数组中一种数出现奇数次和两种数出现奇数次
- Java配置环境变量、方法和原因
- TADVAlertWindow
- Python【每日一问】35
- 35.MySQL 常见问题
- 一天搞懂深度学习—学习笔记2(CNN)
- Xweibo2.0nbsp;游客可以访问任何页面【…
- 微信小程序学习笔记4
- iphone通讯录导入excel
- 为什么计算机中整数的范围是-32768~32767而不是-32767~32767
- 送人玫瑰,手有余香!
- 传递VB数组给DLL中的函数
- C#事件中sender的小用法(转载)
- 怎么用黑白打印机打印清晰可读的PPT文档
- Fiddler 学习笔记--Fiddler 教程
热门文章
- GridView用法详解
- poj 2151 Check the difficulty of problems
- 自动判断浏览器的中英文版本自动跳转网站中英文页面代码
- c# winform程序调用托管dll(c#的dll),使用添加引用和动态加载dll
- “【第二届】Erlang Fans交流会”议程
- linux c编程操作数据库(sqlite3应用)
- MySQL(9)主从复制与读写分离
- python中popen转变时区_python中的subprocess.Popen()使用
- golang mysql大量写入_Golang 实现分片读取http超大文件流和并发控制
- Raspberry Pi 3 计算模块,CPU性能提高了十倍