简体   繁体   English

ConfigureAwait:处理异常的线程在哪?

[英]ConfigureAwait: On which thread is the exception handled?

When you await a Task , the continuation by default runs on the same thread. await Task ,默认情况下,继续运行在同一个线程上。 The only time you ever actually need this is if you're on the UI thread, and the continuation needs to run on the UI thread as well. 您实际需要的唯一时间是,如果您在UI线程上,并且延续也需要在UI线程上运行。

You can control this by using ConfigureAwait , eg: 您可以使用ConfigureAwait来控制它,例如:

await SomeMethodAsync().ConfigureAwait(false);

...which can be useful to offload work from the UI thread that doesn't need to run there. ...这对于从不需要在那里运行的UI线程卸载工作非常有用。 (But see Stephen Cleary's comment below.) (但请参阅Stephen Cleary在下面的评论。)

Now consider this bit of code: 现在考虑这段代码:

try
{
    await ThrowingMethodAsync().ConfigureAwait(false);
}
catch (Exception e)
{
    // Which thread am I on now?
}

And how about this? 那怎么样?

try
{
    await NonThrowingMethodAsync().ConfigureAwait(false);

    // At this point we *may* be on a different thread

    await ThrowingMethodAsync().ConfigureAwait(false);
}
catch (Exception e)
{
    // Which thread am I on now?
}

The exception will be on whatever thread the continuation would have happened on had there been no exception. 如果没有异常,那么异常将在继续发生的任何线程上。

try
{
    await ThrowingMethodAsync().ConfigureAwait(false);
}
catch (Exception e)
{
    // Which thread am I on now?
    //A: Likely a Thread pool thread unless ThrowingMethodAsync threw 
    //   synchronously (without a await happening first) then it would be on the same
    //   thread that the function was called on.
}

try
{
    await NonThrowingMethodAsync().ConfigureAwait(false);

    // At this point we *may* be on a different thread

    await ThrowingMethodAsync().ConfigureAwait(false);
}
catch (Exception e)
{
    // Which thread am I on now?
    //A: Likely a Thread pool thread unless ThrowingMethodAsync threw 
    //   synchronously (without a await happening first) then it would be on the same
    //   thread that the function was called on.
}

For some more clarity: 为了更清晰:

private async Task ThrowingMethodAsync()
{
    throw new Exception(); //This would cause the exception to be thrown and observed on 
                           // the calling thread even if ConfigureAwait(false) was used.
                           // on the calling method.
}

private async Task ThrowingMethodAsync2()
{
    await Task.Delay(1000);
    throw new Exception(); //This would cause the exception to be thrown on the SynchronizationContext
                           // thread (UI) but observed on the thread determined by ConfigureAwait
                           // being true or false in the calling method.
}

private async Task ThrowingMethodAsync3()
{
    await Task.Delay(1000).ConfigureAwait(false);
    throw new Exception(); //This would cause the exception to be thrown on the threadpool
                           // thread but observed on the thread determined by ConfigureAwait
                           // being true or false in the calling method.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在已经在线程池线程上运行的调用上配置等待? - ConfigureAwait on a call which is already running on a threadpool thread? Leetcode 声称我处理的异常未处理 - Leetcode claims that exception is not handled which I handled UI.跨线程操作异常后的Task.ConfigureAwait行为 - Task.ConfigureAwait behavior after UI cross-thread operation exception ConfigureAwait 将延续推送到池线程 - ConfigureAwait pushes the continuation to a pool thread 没有ConfigureAwait(false)的await在另一个线程上继续 - await without ConfigureAwait(false) continues on a different thread 从另一个线程调用一个方法或从另一个线程触发一个在主线程上处理的事件 - Hot to call a method from another thread or fire a event from another thread which gets handled on the main thread 与Task.ConfigureAwait进行线程同步(false) - Thread synchronization with Task.ConfigureAwait(false) 即使被调用方方法在内部执行ConfigureAwait(false),ConfigureAwait(true)始终返回原始线程吗? - Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? 处理的异常仍然被抛出? - Exception that is handled is still thrown? 断言已引发处理的异常 - Assert a handled exception was thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM