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. /// This service is designed to return a TaskScheduler for application's main, UI thread.
  3. /// This service MUST be instantiated on UI thread.
  4. /// </summary>
  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. static UITaskSchedulerService()
  12. {
  13. GuiThread = Thread.CurrentThread;
  14. TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext();
  15. }
  16. /// <summary>
  17. /// Gets the instance.
  18. /// </summary>
  19. public static UITaskSchedulerService Instance
  20. {
  21. get
  22. {
  23. return InstanceField;
  24. }
  25. }
  26. /// <summary>
  27. /// Get TaskScheduler to schedule Tasks on UI thread.
  28. /// </summary>
  29. /// <returns>TaskScheduler to schedule Tasks on UI thread.</returns>
  30. public TaskScheduler GetUITaskScheduler()
  31. {
  32. return TaskSchedulerUI;
  33. }
  34. /// <summary>
  35. /// Check whether current tread is UI tread
  36. /// </summary>
  37. /// <returns><c>true</c>if current tread is UI tread.</returns>
  38. public bool IsOnUIThread()
  39. {
  40. return GuiThread == 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 the TaskScheduler.FromCurrentSynchronizationContext. and from the TaskScheduler Class from 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()
  11. )
  12. //.ContinueWith(
  13. // (task) => ProcessResults(task.Result),
  14. // TaskScheduler.FromCurrentSynchronizationContext())
  15. .LogTaskExceptionIfAny(Log)
  16. .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相关推荐

  1. 遗传算法的简单介绍以及模式定理的简单证明

    遗传算法   遗传算法(Genetic Algorithm,GA),最早是由美国的John holland在20世纪70年代提出.算法通过模拟达尔文生物进化论的自然选择以及遗传学机理的生物进化过程来搜 ...

  2. 2021年大数据ELK(十八):Beats 简单介绍和FileBeat工作原理

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 Beats 简单介绍和FileBeat工作原理 一.Beats 二.FileB ...

  3. 2021年大数据ELK(十五):Elasticsearch SQL简单介绍

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 Elasticsearch SQL简单介绍 一.SQL与Elasticsear ...

  4. 2021年大数据ELK(二):Elasticsearch简单介绍

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 一.Elasticsearch简介 1.介绍 2.创始人 二.E ...

  5. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇-多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  6. 简单介绍一下R中的几种统计分布及常用模型

    统计学上分布有很多,在R中基本都有描述.因能力有限,我们就挑选几个常用的.比较重要的简单介绍一下每种分布的定义,公式,以及在R中的展示. 统计分布每一种分布有四个函数:d――density(密度函数) ...

  7. LVS(Linux Virtual Server)三种负载均衡模型和十种调度的简单介绍

    LVS(Linux Virtual Server)三种负载均衡模型和十种调度的简单介绍 LVS (Linux Virtual Server) LVS(Linux Virtual Server)其实就是 ...

  8. dubbo学习过程、使用经验分享及实现原理简单介绍

    一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...

  9. iOS开发UI篇—UIWindow简单介绍

    iOS开发UI篇-UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...

最新文章

  1. libevent中的hash表
  2. java两个日期之间的日期_获取两个日期之间的日期形成一个集合
  3. MongoDB安装与副本集配置
  4. oracle+测试权限,Oracle测试题
  5. 视频通信关键技术探索及实践
  6. 数组中一种数出现奇数次和两种数出现奇数次
  7. Java配置环境变量、方法和原因
  8. TADVAlertWindow
  9. Python【每日一问】35
  10. 35.MySQL 常见问题
  11. 一天搞懂深度学习—学习笔记2(CNN)
  12. Xweibo2.0nbsp;游客可以访问任何页面【…
  13. 微信小程序学习笔记4
  14. iphone通讯录导入excel
  15. 为什么计算机中整数的范围是-32768~32767而不是-32767~32767
  16. 送人玫瑰,手有余香!
  17. 传递VB数组给DLL中的函数
  18. C#事件中sender的小用法(转载)
  19. 怎么用黑白打印机打印清晰可读的PPT文档
  20. Fiddler 学习笔记--Fiddler 教程

热门文章

  1. GridView用法详解
  2. poj 2151 Check the difficulty of problems
  3. 自动判断浏览器的中英文版本自动跳转网站中英文页面代码
  4. c# winform程序调用托管dll(c#的dll),使用添加引用和动态加载dll
  5. “【第二届】Erlang Fans交流会”议程
  6. linux c编程操作数据库(sqlite3应用)
  7. MySQL(9)主从复制与读写分离
  8. python中popen转变时区_python中的subprocess.Popen()使用
  9. golang mysql大量写入_Golang 实现分片读取http超大文件流和并发控制
  10. Raspberry Pi 3 计算模块,CPU性能提高了十倍