ConfigureAwait:在哪个线程异常处理? [英] ConfigureAwait: On which thread is the exception handled?

查看:139
本文介绍了ConfigureAwait:在哪个线程异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的await A 工作,在同一线程上的延续默认运行。你是否真的需要这唯一的一次是,如果你是在UI线程上,并延续需要在UI线程上以及运行。



您可以控制该通过使用 ConfigureAwait ,例如:

 等待SomeMethodAsync()ConfigureAwait(假); 



...它可以卸载的,这并不需要运行UI线程的工作有用那里。 (不过看到斯蒂芬克利的评论下文。)



现在考虑这段代码:

 
{
等待ThrowingMethodAsync()ConfigureAwait(假)。
}
赶上(例外五)
{
//哪个线程现在我是谁?
}

和这个怎么样?

 
{
等待NonThrowingMethodAsync()ConfigureAwait(假)。

//在这一点上,我们可以* *是在不同的线程

等待ThrowingMethodAsync()ConfigureAwait(假)。
}
赶上(例外五)
{
//哪个线程现在我是谁?
}


解决方案

的异常将是什么螺纹延续会发生在已经有过也不例外。

 
{
等待ThrowingMethodAsync( ).ConfigureAwait(假);
}
赶上(例外五)
{
//哪个线程现在我是谁?
//答:可能是一个线程池线程,除非ThrowingMethodAsync扔
//同步(没有的await第一发生),那么这将是对同一
//线程调用函数上。
}


{
等待NonThrowingMethodAsync()ConfigureAwait(假)。

//在这一点上,我们可以* *是在不同的线程

等待ThrowingMethodAsync()ConfigureAwait(假)。
}
赶上(例外五)
{
//哪个线程现在我是谁?
//答:可能是一个线程池线程,除非ThrowingMethodAsync扔
//同步(没有的await第一发生),那么这将是对同一
//线程调用函数上。
}



对于一些更清楚:

 专用异步任务ThrowingMethodAsync()
{
抛出新的异常(); //这将导致抛出异常,并在
//即使使用ConfigureAwait(假)调用线程观察。
//的调用方法。
}

私人异步任务ThrowingMethodAsync2()
{
等待Task.Delay(1000);
抛出新的异常(); //这将导致要对的SynchronizationContext
//线程(UI)抛出的异常,但观察到ConfigureAwait
//是在调用方法真或假确定的线程上。
}

私人异步任务ThrowingMethodAsync3()
{
等待Task.Delay(1000).ConfigureAwait(假);
抛出新的异常(); //这将导致要在线程池
//线程抛出的异常,但通过ConfigureAwait
//是在调用方法真或假确定的线程上观察到。
}


When you await a Task, the continuation by default runs on the same thread. 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.

You can control this by using ConfigureAwait, e.g.:

await SomeMethodAsync().ConfigureAwait(false);

...which can be useful to offload work from the UI thread that doesn't need to run there. (But see Stephen Cleary's comment below.)

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

这篇关于ConfigureAwait:在哪个线程异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆