Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SynchronizationContext-奇怪的行为 publicstaticvoidinit() { //var task=GetSource1(); //var task=GetSource2(); //var task=GetSource3(); var task=MyClient(); MessageBox.Show(task.Result); } 专用静态异步任务GetSource1() { var sourceTask=WebClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务GetSource2() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(true); 返回等待源任务; } 专用静态异步任务GetSource3() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务WebClient() { 返回等待新WebClient()。下载StringTaskAsync(“http://4pda.ru“”。配置等待(错误); } 私有静态任务MyClient() { var t=新任务(()=>新WebClient()。下载字符串(“http://4pda.ru")); t、 配置等待(false); t、 Start(); 返回t; }_C#_.net_Async Await_Task Parallel Library_Synchronizationcontext - Fatal编程技术网

C# SynchronizationContext-奇怪的行为 publicstaticvoidinit() { //var task=GetSource1(); //var task=GetSource2(); //var task=GetSource3(); var task=MyClient(); MessageBox.Show(task.Result); } 专用静态异步任务GetSource1() { var sourceTask=WebClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务GetSource2() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(true); 返回等待源任务; } 专用静态异步任务GetSource3() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务WebClient() { 返回等待新WebClient()。下载StringTaskAsync(“http://4pda.ru“”。配置等待(错误); } 私有静态任务MyClient() { var t=新任务(()=>新WebClient()。下载字符串(“http://4pda.ru")); t、 配置等待(false); t、 Start(); 返回t; }

C# SynchronizationContext-奇怪的行为 publicstaticvoidinit() { //var task=GetSource1(); //var task=GetSource2(); //var task=GetSource3(); var task=MyClient(); MessageBox.Show(task.Result); } 专用静态异步任务GetSource1() { var sourceTask=WebClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务GetSource2() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(true); 返回等待源任务; } 专用静态异步任务GetSource3() { var sourceTask=MyClient(); sourceTask.ConfigureAwait(false); 返回等待源任务; } 专用静态异步任务WebClient() { 返回等待新WebClient()。下载StringTaskAsync(“http://4pda.ru“”。配置等待(错误); } 私有静态任务MyClient() { var t=新任务(()=>新WebClient()。下载字符串(“http://4pda.ru")); t、 配置等待(false); t、 Start(); 返回t; },c#,.net,async-await,task-parallel-library,synchronizationcontext,C#,.net,Async Await,Task Parallel Library,Synchronizationcontext,这段代码运行良好。我在消息框中获取源代码。但是,为什么在使用var task=GetSource3()时出现死锁?我认为它一定能工作,因为我使用了ConfigureAwait(false),避免了上下文切换ConfigureAwait是一种纯粹的方法。它返回一个值,但对其本身没有影响。它返回您需要等待的自定义可等待(ConfiguredTaskAwaitable): public static void Init() { //var task = GetSource1(); //

这段代码运行良好。我在
消息框中获取源代码。但是,为什么在使用
var task=GetSource3()时出现死锁?我认为它一定能工作,因为我使用了
ConfigureAwait(false)
,避免了上下文切换
ConfigureAwait
是一种纯粹的方法。它返回一个值,但对其本身没有影响。它返回您需要等待的自定义可等待(
ConfiguredTaskAwaitable
):

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;
}

然而,这不是处理死锁的方式。这是一种预防措施,但首先应该避免阻塞异步代码。

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

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;
}

然而,这不是处理死锁的方式。这是一种预防措施,但首先应该避免在异步代码上阻塞。

您应该通过异步执行所有操作来避免死锁,而不是将continuations推送到线程池线程。您几乎不应该使用任务构造函数。如果要将工作卸载到线程池线程,请使用
Task.Run
这是一个测试示例。我得到了下面的答案。感谢您应该通过异步执行所有操作来避免死锁,而不是将continuations推送到线程池线程。您几乎不应该使用任务构造函数。如果要将工作卸载到线程池线程,请使用
Task.Run
这是一个测试示例。我得到了下面的答案。感谢换句话说:这是配置等待,而不是配置任务。换句话说:这是配置等待,而不是配置任务。