c#语言简介_简单介绍自己

c#语言简介_简单介绍自己taskScheduler根据定义ThetaskSchedulerbythedefinitionblurb.“Istheclasswheretheusagecontextiswithinthetasklibraries.“它的作用像是WPF/Winform时代的SynchronizationContext.ItisliketheSync…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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.   
  12.     static UITaskSchedulerService()  
  13.     {  
  14.         GuiThread = Thread.CurrentThread;  
  15.         TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext();  
  16.     }  
  17.   
  18.     /// <summary>  
  19.     /// Gets the instance.  
  20.     /// </summary>  
  21.     public static UITaskSchedulerService Instance  
  22.     {  
  23.         get  
  24.         {  
  25.             return InstanceField;  
  26.         }  
  27.     }  
  28.   
  29.     /// <summary>  
  30.     /// Get TaskScheduler to schedule Tasks on UI thread.  
  31.     /// </summary>  
  32.     /// <returns>TaskScheduler to schedule Tasks on UI thread.</returns>  
  33.     public TaskScheduler GetUITaskScheduler()  
  34.     {  
  35.         return TaskSchedulerUI;  
  36.     }  
  37.   
  38.     /// <summary>  
  39.     /// Check whether current tread is UI tread  
  40.     /// </summary>  
  41.     /// <returns><c>true</c>if current tread is UI tread.</returns>  
  42.     public bool IsOnUIThread()  
  43.     {  
  44.         return GuiThread == Thread.CurrentThread;  
  45.     }  
  46. }  

 

该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://javaforall.net/189598.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CSS检测的高像素密度屏幕设备[通俗易懂]

    CSS检测的高像素密度屏幕设备

    2022年1月17日
    47
  • python动态心形代码简单_python心形曲线代码

    python动态心形代码简单_python心形曲线代码欢迎关注【拇指笔记】,持续更新一些好玩、有趣的Python小项目。需要程序的朋友,直接私信我即可。原文链接【Python】五分钟画一条动态心形曲线~​mp.weixin.qq.com先上效果使用Python绘制一条动态的心形曲https://www.zhihu.com/video/12280010884537671681.绘制一条心形曲线最近看到个视频,打算绘制个心型动态的曲线。1.1Matla…

    2022年10月16日
    2
  • web安全色_WEB标准颜色

    web安全色_WEB标准颜色web安全色产生的原因不同的平台(Mac、PC等)有不同的调色板,不同的浏览器也有自己的调色板。这就意味着对于一幅图,显示在Mac上的Web浏览器中的图像,与它在PC上相同浏览器中显示的效果可能差别很

    2022年8月1日
    7
  • maven本地仓库默认路径_maven本地仓库

    maven本地仓库默认路径_maven本地仓库Maven配置本地仓库的路径1.打开maven安装路径\conf\settings.xml2.在50行左右有下面,默认位置在c盘用户下的/.m2/repository中<!–localRepository|Thepathtothelocalrepositorymavenwillusetostoreartifacts.||Defa…

    2022年9月23日
    2
  • pycharm打不出中文_pycharm语言怎么改成中文

    pycharm打不出中文_pycharm语言怎么改成中文pycharm不能输入中文解决办法

    2025年8月15日
    2
  • eureka底层原理「建议收藏」

    eureka底层原理「建议收藏」什么是注册中心全称为:服务注册与发现,rpc远程调用框架核心思想,在于注册中心,使用注册中心管理每个服务与服务之间的依赖关系,这种关系被称为服务治理概念;任何rpc远程调用框架都至少有一个注册中心;服务注册将服务信息注册到注册中心,相当于告诉公司的人,我已经打卡上班了,可以分配工作任务给我了,比如现在我们有一个服务消费者服务A,和两个节点的服务提供者,服务B。服务A和服务B在启动的时候都会向注册中心进行服务注册。服务发现从注册中心获取已注册的服务信息,发现有哪些可以调用的服务可供我使用;相

    2022年10月7日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号