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


相关推荐

  • f1-score是什么_python概念题

    f1-score是什么_python概念题一、F1score概念?F1score是分类问题的一个衡量指标,一些多分类问题的机器学习竞赛,常把F1score作为最终评测的方法。它是精确率和召回率的调和平均数,取值0-1之间。F1score认为召回率和精确率同样重要,而F2认为召回率的重要程度是精确率的2倍,F0.5则认为召回率的重要程度是精确率的一半。要明确几个概念TP(TruePositive):被判定为正样本,实际为正样本 TN(TrueNegative):被判定为负样本,实际为负样本 FP(FalseP

    2022年10月14日
    2
  • 多元logistic回归模型——spss步骤

    多元logistic回归模型——spss步骤多元 因变量为多分类变量 结果在三种及三种以上 如 机构养老 社区养老 居家养老 自变量 可以是分类变量或连续变量 建议是分类变量 协变量 必须是分类变量 案例 步骤 1 分析 回归 多项 logistic 打开主面板 因变量 自变量分别按照箭头指示移入对应的变量框内 点击 参考类别 按钮 默认勾选 最后一个类别 指以因变量和自变量的最后一个分类水平为参照 用其他分类依次与之对比 考察不同水平间的倾向 2 主面板中 点击 模型 打开 多项 logistic 回归 模型 对话框 勾

    2025年7月5日
    1
  • NOIp2011数字反转「建议收藏」

    NOIp2011数字反转「建议收藏」题解:本题考查循环的使用和用一个数字表示一个多位数。代码如下:“`cppincludeusingnamespacestd;intmain(){intn,a=0;cin&amp;amp;amp;gt;&amp;amp;amp;gt;n;while(n!=0){a=a*10+n%10;n=n/10;}cou…

    2022年9月24日
    2
  • 多项式曲线拟合之最小二乘法推导[通俗易懂]

    多项式曲线拟合之最小二乘法推导[通俗易懂]1、多项式曲线拟合之最小二乘法1.1问题来源1801年,意大利天文学家朱赛普·皮亚齐发现了第一颗小行星谷神星。经过40天的跟踪观测后,由于谷神星运行至太阳背后,使得皮亚齐失去了谷神星的位置。随后全世界的科学家利用皮亚齐的已有观测数据开始寻找谷神星,但是根据大多数人计算的结果来寻找谷神星都没有结果。只有时年24岁的高斯所计算的谷神星的轨道,被奥地利天文学家海因里希·奥尔伯斯的观测所证实,使天文界从此可以预测到谷神星的精确位置。同样的方法也产生了哈雷彗星等很多天文学成果。高斯使用的方法就是最小二乘法,

    2022年5月17日
    56
  • 运行时常量池与字符串常量池_常量池是什么

    运行时常量池与字符串常量池_常量池是什么文章目录1)方法区包含哪些内容?2)运行时常量池存在什么地方?3)取消永久代后,方法区的实现?4)字符串常量池和运行时常量池究竟去了哪里?4)元空间是什么?5)关于为什么移除永久代?5)补充        我们知道在JDK1.8中取消了永久代,区而代之使用了元空间来实现方法区。话虽如此,但是关于字符串常量池和运行时常量池的模棱两可的说法一直都是争论不休的。1)方法区包含哪些内容?方法区包含哪些内容,摘录自《java虚拟机规

    2022年7月28日
    5
  • 学习CURL扩展功能的使用(一)「建议收藏」

    学习CURL扩展功能的使用(一)「建议收藏」其实CURL这个扩展本来也不打算写得,毕竟这个也是大家最常用的功能之一的。不过既然是在刷文档,学习到了就分享出来吧,不要陷入“知识的诅咒”。本身自己的知识体系就不完整,说不定也有很多小伙伴和我一样

    2022年7月2日
    23

发表回复

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

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