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. /// <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.

  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

C# - 简单介绍TaskScheduler的更多相关文章

  1. [原创]关于mybatis中一级缓存和二级缓存的简单介绍

    关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...

  2. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  3. 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍

    一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...

  4. yii2的权限管理系统RBAC简单介绍

    这里有几个概念 权限: 指用户是否可以执行哪些操作,如:编辑.发布.查看回帖 角色 比如:VIP用户组, 高级会员组,中级会员组,初级会员组 VIP用户组:发帖.回帖.删帖.浏览权限 高级会员组:发帖 ...

  5. angular1.x的简单介绍(二)

    首先还是要强调一下DI,DI(Denpendency Injection)伸手获得,主要解决模块间的耦合关系.那么模块是又什么组成的呢?在我看来,模块的最小单位是类,多个类的组合就是模块.关于在根模块 ...

  6. Linux的简单介绍和常用命令的介绍

    Linux的简单介绍和常用命令的介绍 本说明以Ubuntu系统为例 Ubuntu系统的安装自行百度,或者参考http://www.cnblogs.com/CoderJYF/p/6091068.html ...

  7. iOS-iOS开发简单介绍

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...

  8. iOS开发多线程篇—多线程简单介绍

    iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...

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

    iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

随机推荐

  1. Android中如何实现文件下载

        最近做一个项目需要从服务器下载图片到本地sdcard,上网查找了一些例子,下面这个比较合适,原文内容如下:   我们在开发中经常需要从服务器下载文件,下载的内容可能有交换的信息,缓存的图片,程 ...

  2. ECharts 与struts的后台交互之柱状图

    ECharts主页:  http://echarts.baidu.com/index.html ECharts-2.1.8下载地址:  http://echarts.baidu.com/build/e ...

  3. BZOJ 2466 中山市选2009 树 高斯消元+暴力

    题目大意:树上拉灯游戏 高斯消元解异或方程组,对于全部的自由元暴力2^n枚举状态,代入计算 这做法真是一点也不优雅... #include <cstdio> #include <cs ...

  4. 更改Android应用程序的图标

    对于android应用程序的开发.默认的图标是一个小机器人,图片名称为ic_launcher.png. 可是,大多数开发人员是会将这个图标在开发过程中改为自己设计的icon. 把apk图标更改为自己设 ...

  5. [svc]logstash和filebeat之间ssl加密

    cfssl生成证书 wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/local/bin/cfssl wget https://pkg ...

  6. JMeter学习笔记(五)-总结

    本周主要学习了JMeter如下几方面内容: (1)Bdboy录制方式: (2)JMeter的代理录制方式: (3)关联,在关联时我们要找到哪些内容是要关联的,这个主要通过分析哪些内容是由服务器返回的, ...

  7. IAsyncResult 接口

    IAsyncResult 接口由包含可异步操作的方法的类实现.它是启动异步操作的方法的返回类型,如 FileStream.BeginRead,也是结束异步操作的方法的第三个参数的类型,如 FileSt ...

  8. 【ActiveMQ】ActiveMQ在CentOS的搭建与使用

    下载 到ActiveMQ官网,找到下载点. 目前, 官网为http://activemq.apache.org/. 我们下载目前最新的版本吧,当前的Linux版本下载地址之一为:http://apac ...

  9. 给singer的左侧添加fixedTitle,并显示向上滚动偏移效果;

    1.将写好的dom绝对定位到顶部: 2.dom值为singerlist的currentIndex.title(通过计算属性获取),如果有则显示fixedTitle,没有则隐藏: 3.计算diff:当d ...

  10. CEdit 控制键盘操作

    写在前面的话:在工作中要实现这样一个功能,在一个树形控件中选择要发送消息的客户,添加到一个列表控件中,点击确定跳到一个信息发送页面如下图,在发送人编辑框中显示选择的客户,要实现的就是即允许用户删除用户 ...