SynchronizationContext - 奇怪的行为

Ser*_*046 1 .net c# synchronizationcontext task-parallel-library async-await

public static void Init()
{
    //var task = GetSource1();
    //var task = GetSource2();
    //var task = GetSource3();
    var task = MyClient();
    MessageBox.Show(task.Result);
}

private static async Task<string> GetSource1()
{
    var sourceTask = WebClient();
    sourceTask.ConfigureAwait(false);
    return await sourceTask;
}

private static async Task<string> GetSource2()
{
    var sourceTask = MyClient();
    sourceTask.ConfigureAwait(true);
    return await sourceTask;
}

private static async Task<string> GetSource3()
{
    var sourceTask = MyClient();
    sourceTask.ConfigureAwait(false);
    return await sourceTask;
}

private static async Task<string> WebClient()
{
    return await new WebClient().DownloadStringTaskAsync("http://4pda.ru").ConfigureAwait(false);
}

private static Task<string> MyClient()
{
    var t = new Task<string>(() => new WebClient().DownloadString("http://4pda.ru"));
    t.ConfigureAwait(false);
    t.Start();
    return t;
}
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常.我得到了源代码MessageBox.但是为什么我使用时会出现死锁var task = GetSource3()?我认为它必须有效,因为我使用ConfigureAwait(false)并避免上下文切换

i3a*_*non 6

ConfigureAwait是一种纯粹的方法.它返回一个值,但对它自己没有影响.它返回ConfiguredTaskAwaitable您需要等待的自定义等待():

await task.ConfigureAwait();
Run Code Online (Sandbox Code Playgroud)

但是,这不是你应该如何处理死锁.这是一个预防措施,但你应该避免阻止异步代码开始.