zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#语言简介_简单介绍自己

c#语言 简单 介绍 简介 自己
2023-06-13 09:14:58 时间

大家好,又见面了,我是你们的朋友全栈君。

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. static UITaskSchedulerService()
  12. {
  13. GuiThread = Thread.CurrentThread;
  14. TaskSchedulerUI = TaskScheduler.FromCurrentSynchronizationContext();
  15. }
  16. /// <summary>
  17. /// Gets the instance.
  18. /// </summary>
  19. public static UITaskSchedulerService Instance
  20. {
  21. get
  22. {
  23. return InstanceField;
  24. }
  25. }
  26. /// <summary>
  27. /// Get TaskScheduler to schedule Tasks on UI thread.
  28. /// </summary>
  29. /// <returns>TaskScheduler to schedule Tasks on UI thread.</returns>
  30. public TaskScheduler GetUITaskScheduler()
  31. {
  32. return TaskSchedulerUI;
  33. }
  34. /// <summary>
  35. /// Check whether current tread is UI tread
  36. /// </summary>
  37. /// <returns><c>true</c>if current tread is UI tread.</returns>
  38. public bool IsOnUIThread()
  39. {
  40. return GuiThread == Thread.CurrentThread;
  41. }
  42. }

该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.cn/189598.html原文链接:https://javaforall.cn