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)
上一篇 2026年3月12日 上午9:43
下一篇 2026年3月12日 上午9:48


相关推荐

  • OpenClaw本地部署有一键脚本、源码编译方式

    OpenClaw本地部署有一键脚本、源码编译方式

    2026年3月12日
    5
  • Java中dom4j读取配置文件实现抽象工厂+反射

    Java中dom4j读取配置文件实现抽象工厂+反射

    2021年8月24日
    54
  • idea怎么搭建springboot(搭建服务器教程)

    一、创建项目1.File->new->project;2.选择“SpringInitializr”,点击next;(jdk1.8默认即可)3.完善项目信息,组名可不做修改,项目名可做修改;最终建的项目名为:test,src->main->java下包名会是:com->example->test;点击next;4.Web下勾选Spri…

    2022年4月18日
    54
  • java游戏开发实例,吐血整理「建议收藏」

    java游戏开发实例,吐血整理「建议收藏」专题1:JavaOOP1、什么是B/S架构?什么是C/S架构2、Java都有哪些开发平台?3、什么是JDK?什么是JRE?4、Java语言有哪些特点5、面向对象和面向过程的区别6、什么是数据结构?7、Java的数据结构有哪些?8、什么是OOP?9、类与对象的关系?10、Java中有几种数据类型11、标识符的命名规则。12、instanceof关键字的作用13、什么是隐式转换,什么是显式转换14、Char类型能不能转成int类

    2022年7月7日
    44
  • Web前端技术栈

    Web前端技术栈比较全面的浏览器 平台 类库 框架 工具等 Web 前端技术栈 Web 前端开发技术栈 浏览器 InternetExpl Chrome Firefox Safari Opera Edge Netscape 协议 HTTP 1 1 链接 会话 授权 请求 响应 HTTP 2 压缩 打包 服务器端推送 WebSocket Web 三剑客

    2026年3月16日
    5
  • 周金涛:人生就是一次康波

    周金涛:人生就是一次康波代表地产的八运将在 2023 年结束 而房地产的大底也将在那几年前后形成 nbsp 下个九运将是文化的盛世 看看现在一些富豪进军 布局文化 体育 影视产业 应该能明白其中的门道了吧 nbsp 经济是人的行为 人是自然的一部分 既然自然 宇宙有运行规律 那么谁都是命中注定的 nbsp nbsp 无论是研究还是投资 周金涛最大的兴趣始终在于搞清楚周期这件事情 透过周期 不仅能把握经

    2026年3月17日
    2

发表回复

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

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