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


相关推荐

  • 虚拟IP简介「建议收藏」

    虚拟IP简介「建议收藏」什么是虚拟IP虚拟IP(VirtualIPAddress,简称VIP)是一个未分配给真实弹性云服务器网卡的IP地址。弹性云服务器除了拥有私有IP地址外,还可以拥有虚拟IP地址,用户可以通过其中任意一个IP(私有IP/虚拟IP)访问此弹性云服务器。同时,虚拟IP地址拥有私有IP地址同样的网络接入能力,包括VPC内二三层通信、VPC之间对等连接访问,以及弹性公网IP、VPN、云专线等网络接入。多个主备部署的弹性云服务器可以在绑定虚拟IP地址时选择同一个虚拟IP地址。用户可以为该虚拟IP地址绑定一个弹

    2022年10月20日
  • 如何设置VSCode删除整行快捷键?

    如何设置VSCode删除整行快捷键?如何设置VSCode删除整行快捷键?在使用VSCode的时候,发现它的快捷键是Ctrl+Shift+K那怎么设置成我们经常用的Ctrl+D呢1.选择文件–>首选项–>键盘快捷方式2.输入CtrlD,对应的是将下一个查找匹配项添加到选择,点击前面的编辑,将快捷键改成(Ctrl+Shift+Alt+D)改成一个不会与现有的快捷键重复就行;3.输入CtrlShiftK找到原来的删除整行快捷命令,将其改成Ctrl+D,就可以愉快地删除了…

    2022年6月15日
  • origin柱状图显示具体数据值

    origin柱状图显示具体数据值1.双击需要显示数据的那一列比如双击蓝色这一列,所有的这一列数据都会选中2.打开绘图细节-绘图属性,选中标签3.点击启用4.选择标签形式’Y’5.选择数值显示格式*3*即可显示,然后调整位置即可…

    2022年9月30日
  • mac navicat激活码【2021免费激活】

    (mac navicat激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZPB5EL5Q-eyJsaWNlb…

    2022年3月21日
  • Druid连接池的意义以及使用

    Druid连接池的意义以及使用建立数据库连接耗时耗费资源,一个数据库服务器能够同时建立的连接数也是有限的,在大型的Web应用中,可能同时会有成百上千的访问数据库的请求,如果Web应用程序为每一个客户请求分配一个数据库连接,将导致性能的急剧下降。数据库连接池的意义在于,能够重复利用数据库连接(有点类似线程池的部分意义),提高对请求的响应时间和服务器的性能。连接池中提前预先建立了多个数据库连接对象,然后将连接对象保存到连接池中…

    2022年7月23日
  • Win32 API 常量定义(四)

    PublicConstMIXER_OBJECTF_HMIDIOUT=(MIXER_OBJECTF_HANDLEOrMIXER_OBJECTF_MIDIOUT)PublicConstMIXER_OBJECTF_MIDIIN=&H40000000PublicConstMIXER_OBJECTF_HMIDIIN=(MIXER_OBJECTF_HANDLEOrMIX

    2022年4月8日

发表回复

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

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