C#-TaskSchedulerの簡単な説明

4425 ワード

タイトル:C#-TaskSchedulerTitleの簡単な説明:C#-A Brief bump to the TaskSchedulertask 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());

処理結果を正しく表示するには、UI threadにdispatchが必要です.
 
We need to dispatch the process result back to the UI thread so that display can be properly handled.
References:
TaskScheduler Class