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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js获取当前时间(特定的时间格式)[通俗易懂]

    js获取当前时间(特定的时间格式)[通俗易懂]在一个程序中需要对用户的操作进行记录,记录其操作信息,需要对操作进行归类,有时候用户的操作是重复性的操作,那对于重复的操作,也是要区分的,方便查找,可以通过设置类似GUID的唯一值,也可以获取当前的操作时间来区分,因为时间也是唯一的,在任何时候时间都不会出现重复,当然可以获取就可以设置,所以您也可以人为的去设置/修改操作时间。但是这样不好。使用javascript获取时间:在javas…

    2022年9月15日
    2
  • pdf.js使用方法「建议收藏」

    pdf.js使用方法「建议收藏」项目中显示pdf的功能,浏览过不少的技术帖,都不太理想,花了点时间研究了下pdf.js正确使用方法,总结下:1.防止自己忘记2.工作留有痕迹3.供大家参考借鉴pdf.js:将PDF文件解析后生成一张.png图片,利用canvas元素显示在页面上,此方法不推荐使用,呈现在页面上的pdf会模糊,目前没有找到有效解决办法,给爱钻研的小伙伴提供个思路,在pdf.js官网上有…

    2022年7月11日
    22
  • 微创业平台

    一种专为大学生创业服务的平台。主旨是,让大学生创业创新,在某种比较简单,比较容易的环境里进行。让大学生创业创新,不要太多的风险,比较容易达成创业目标。云电话,一种5G新电话。特别适合大学生微创业,更需要大学生微创新。将云电话平台,建设成湖铁微创业平台,是深圳云电话平台,与湖铁职院创新创业学院,合作打造的专为湖铁职院大学生,创新创业服务的平台。建设好湖铁微创业平台,具有重大的社会意义。√成功示范作用。先在创新创业学院,打造标杆试点MVP。√便…

    2022年4月4日
    87
  • 电容触摸屏GT911、GT928、GT9147的使用

    电容触摸屏GT911、GT928、GT9147的使用一、介绍与硬件连接GT911、GT928、GT9147都属于GT9系列非单层多点触控芯片,他们支持的触控点数不同(GT928支持10个点、GT911支持5个点)、驱动和感应通道也可能不同。可是他们的寄存器和IIC通讯时序是相同的,也就是说驱动程序是兼容的。与主机的接口共有6PIN,分别为:VDD、GND、SCL、SDA、INT、RESET。INT、RESET…

    2022年6月29日
    139
  • Prism初研究之Bootstrapper

    Prism初研究之BootstrapperPrism初研究之初始化应用Prism初研究之初始化应用BootstrapperDIShell关键抉择核心步骤创建Bootstrapper实现CreateShell方法实现InitializeShell方法创建并配置ModuleCatalog创建并配置Container核心服务(与应用无关)与应用相关的服务(StockTraderRI)在UnityBootstrapper中创建并配置…

    2022年7月20日
    17
  • 中标麒麟配置本地yum源_优麒麟系统安装

    中标麒麟配置本地yum源_优麒麟系统安装在linux系统上,解决软件包之间的依赖关系是很重要的事。很多工作无法实现可能就是因为缺少一个软件包,而当你千方百计找到这个软件包的时候,却发现它跟当前系统不兼容。所以,要做的非常重要的一件事情就是给系统添加软件仓库,以确保能安装使用大部分软件包。(亲测)建议看完文章再动手配置实验环境:[1-06@localhostDesktop]$uname-aLinuxlocalh…

    2022年8月10日
    158

发表回复

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

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