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


相关推荐

  • 5G网络注册中的切片选择「建议收藏」

    5G网络注册中的切片选择「建议收藏」UE->gNB开机以后,UE会发送RequestedNSSAI注册申请,会带有请求的切片ID,即这次的注册我想请求哪些切片ID,发送给基站,gNB->InitialAMF基站RequestedNSSAI注册申请,基站会根据之前的(),去选择一个AMF,初始AMFInitaialAMF可以看到手机带上来的切片ID到底是什么(例如1号),3.InitialAMF<–>UDM同时InitaialAMF会拿手机UE的IMSI信息,在5G里面是SUPI

    2022年10月2日
    0
  • Vue高阶组件_高阶组件的承上启下

    Vue高阶组件_高阶组件的承上启下目录一、高阶组件概念二、目标三、思路四、准备五、实现六、难点Reference一、高阶组件概念何谓高阶组件?类比高阶函数的定义:将函数作为参数的函数就是高阶函数,那么,将组件作为参数的组件就是高阶组件。二、目标假如我们有一个组件,我们希望通过某个函数,去扩展它,得到一个新的组件,新的组件有完全的参数组件的行为,如果这点可以满足,那么其他扩展就可以针对性的…

    2025年7月27日
    2
  • Python小白的10个常犯错误,你还在这样操作吗?

    Python小白的10个常犯错误,你还在这样操作吗?

    2021年7月8日
    75
  • HTML文本载入HTMLDocument2进行解析

    HTML文本载入HTMLDocument2进行解析更多精彩内容,请见:http://www.16boke.comIHTMLDocument2*pDoc;IHTMLElementCollection*objAllElement;HRESULThr;CoInitialize(NULL);hr=CoCreateInstance(CLSID_HTMLDocument,NULL,CLSCTX_INPROC_SERVER,IID_IHTMLDocument2,(void**)&pDoc);

    2022年7月19日
    12
  • HashMap常见面试题_java面试题大汇总

    HashMap常见面试题_java面试题大汇总1.HashMap的数据结构?哈希表结构(链表散列:数组+链表)实现,结合数组和链表的优点。当链表长度超过8时,链表转换为红黑树。2.HashMap的工作原理?HashMap底层是hash数组和单向链表实现,数组中的每个元素都是链表,由Node内部类(实现Map.Entry<K,V>接口)实现,HashMap通过put&get方法存储和获取。存储对象时,将K/V键值传给put()方法: ①、调用hash(K)方法计算K的hash值,然后结合数组长度,计算得数组下标;

    2022年8月10日
    7
  • tkMapper的andLike的使用「建议收藏」

    tkMapper的andLike的使用「建议收藏」查询条件:wheretype=’intf’and(codelike’%keyword%’ornamelike’%keyword%’)Exampleexample=newExample(SysPermissionEntity.class);example.createCriteria().andEqualTo(“type”,”intf”).andLike(“code”,”%”+keyword+”%”);List<SysPermissionEntity>per.

    2022年10月6日
    0

发表回复

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

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