了解TaskScheduler.Current的行为

时间:2014-04-14 22:35:59

标签: c# .net task-parallel-library async-await

这是一个简单的WinForms应用程序:

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var ts = TaskScheduler.FromCurrentSynchronizationContext();
            await Task.Factory.StartNew(async () =>
            {
                Debug.WriteLine(new
                {
                    where = "1) before await",
                    currentTs = TaskScheduler.Current,
                    thread = Thread.CurrentThread.ManagedThreadId,
                    context = SynchronizationContext.Current
                });

                await Task.Yield(); // or await Task.Delay(1)

                Debug.WriteLine(new
                {
                    where = "2) after await",
                    currentTs = TaskScheduler.Current,
                    thread = Thread.CurrentThread.ManagedThreadId,
                    context = SynchronizationContext.Current
                });

            }, CancellationToken.None, TaskCreationOptions.None, scheduler: ts).Unwrap();
        }
    }
}

调试输出(单击按钮时):

{ where = 1) before await, currentTs = System.Threading.Tasks.SynchronizationContextTaskScheduler, thread = 9, context = System.Windows.Forms.WindowsFormsSynchronizationContext }
{ where = 2) after await, currentTs = System.Threading.Tasks.ThreadPoolTaskScheduler, thread = 9, context = System.Windows.Forms.WindowsFormsSynchronizationContext }

问题:为什么TaskScheduler.Current在此SynchronizationContextTaskScheduler之后ThreadPoolTaskSchedulerawait更改为TaskCreationOptions.HideScheduler

这基本上展示了await延续的行为{{1}},这在我看来是出乎意料的和不受欢迎的。

这个问题是由我的另一个问题引发的:

AspNetSynchronizationContext and await continuations in ASP.NET

1 个答案:

答案 0 :(得分:12)

如果没有执行实际的任务,则TaskScheduler.CurrentTaskScheduler.Default相同。换句话说,ThreadPoolTaskScheduler实际上既作为线程池任务调度程序作为值意义"没有当前任务调度程序"。

使用async显式调度SynchronizationContextTaskScheduler委托的第一部分,并在UI线程上运行任务调度程序和同步上下文。任务调度程序将委托转发到同步上下文。

await捕获其上下文时,它捕获同步上下文(而不是任务调度程序),并使用该syncctx恢复。因此,方法continuation被发布到syncctx,它在UI线程上执行它。

当延续在UI线程上运行时,它的行为与事件处理程序非常相似;委托直接执行,不包含在任务中。如果您在TaskScheduler.Current开头检查button1_Click,则会发现它也是ThreadPoolTaskScheduler

顺便说一句,我建议您将此行为(直接执行委托,不包含在任务中)视为实现细节。

相关问题