标题: C# - 简单介绍TaskScheduler
Title:?C# - A Brief bump to the TaskScheduler
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初始化。
