大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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.
- /// <summary>
- /// This service is designed to return a TaskScheduler for application’s main, UI thread.
- /// This service MUST be instantiated on UI thread.
- /// </summary>
- [DebuggerNonUserCode]
- public class UITaskSchedulerService : IUITaskSchedulerService
- {
- private static readonly UITaskSchedulerService InstanceField = new UITaskSchedulerService();
- private static readonly TaskScheduler TaskSchedulerUI;
- private static readonly Thread GuiThread;
- static UITaskSchedulerService()
- {
- GuiThread = Thread.CurrentThread;
- TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext();
- }
- /// <summary>
- /// Gets the instance.
- /// </summary>
- public static UITaskSchedulerService Instance
- {
- get
- {
- return InstanceField;
- }
- }
- /// <summary>
- /// Get TaskScheduler to schedule Tasks on UI thread.
- /// </summary>
- /// <returns>TaskScheduler to schedule Tasks on UI thread.</returns>
- public TaskScheduler GetUITaskScheduler()
- {
- return TaskSchedulerUI;
- }
- /// <summary>
- /// Check whether current tread is UI tread
- /// </summary>
- /// <returns><c>true</c>if current tread is UI tread.</returns>
- public bool IsOnUIThread()
- {
- return GuiThread == Thread.CurrentThread;
- }
- }
该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.
- Task.Factory
- .StartNew(
- () =>
- _riskProvider.GetRiskPnL(),
- CancellationToken.None,
- TaskCreationOptions.None,
- TaskScheduler.Default)
- .ContinueWith(
- (task) => ProcessResults(task.Result),
- UITaskSchedulerService.Instance.GetUITaskScheduler()
- )
- //.ContinueWith(
- // (task) => ProcessResults(task.Result),
- // TaskScheduler.FromCurrentSynchronizationContext())
- .LogTaskExceptionIfAny(Log)
- .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:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189598.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...