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. 在程序开发中怎样写SQL语句可以提高数据库的性能

    以下内容是公司dba总结. 1. 首先要搞明白什么叫执行计划?   执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来 ...

  2. Python2 cmp() 函数

    描述 cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1. 语法 以下是 cmp() 方法的语法: cmp( ...

  3. ECSHOP后台权限分配原理分析

    1.在权限管理->管理员列表,中添加一名管理员 2.在admin\includes\inc_menu.php $modules['02_cat_and_goods']['01_goods_lis ...

  4. php序列化与反序列化时字符集不一致问题的解决办法

    今天的用PHP的时候无意的出现了用unserialize()函数转换老是返回false,我确认我的字符串是没错的,测试了很多次还是一样,没办法,启用了error_reporting(E_ALL)启用错 ...

  5. unity 获得子节点

    transform.FindChild("childName") transform.FindChild("childName/grandChildName") ...

  6. linux shell 命令获取字符串/文件的MD5值

    获取字符串的MD5值: 字符串“hello”的MD5: $ echo -n 'hello'|md5sum|cut -d ' ' -f1 得到的MD5值: 5d41402abc4b2a76b9719d9 ...

  7. storm配置:如何解决worker进程内存过小的问题

    问题导读1.如何设置storm内存?2.如果没有配置文件的情况下,该如何配置一些参数?3.通过哪个参数可以配置内存? Storm中真正干活的是各个worker,而worker由supervisor负责 ...

  8. CentOS 7 设置中文环境

    在vultr上的虚拟机虽然安装了中文支持,但是默认显示英语. 只要修改 /etc/locale.conf 即可. LANG="zh_CN.UTF-8" LANGUAGE=" ...

  9. 通过GUID生成可持久化的PID

    byte[] buffer = Guid.NewGuid().ToByteArray(); ); GUID是微软针对UUID的实现,直接生成会大于long类型的最大长度. 但只要转换一下即可

  10. [sh]sed 4个功能

    [root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...