How to guarantee continuation of asynchronous method continuation in another thread?

Does ConfigureAwait(false) provide continuation in another thread, or only signals that it is not required to run in one thread?

Is there any way to provide this guarantee?

I need to test the context thread by thread.

+5
source share
2 answers

Using ConfigureAwait(false) tells awaiter not to resume recording in the captured context, so the SynchronizationContext ignored. This means that a continuation will be scheduled by default for the TaskScheduler , which uses ThreadPool threads.

If the original thread was a ThreadPool thread, the continuation might work on the same thread, otherwise you would guarantee its other thread.

You can run your test using a dedicated thread without a SynchronizationContext (or with ConfigureAwait(false) ) to make sure that the threads are different before and after the async operation.

+8
source

The following is a scenario in which ConfigureAwait(false) does not start a new context.

 Task task2Seconds = Wait2Seconds(); Task task5Seconds = Wait5Seconds(); await task5Seconds; await task2Seconds.ConfigureAwait(false); 

The first await does not have this ConfigureAwait(false) , but it takes longer than the second await , which has this setting, but will be ready to resume in advance. Thus, the second will be resumed in the same context.

0
source

Source: https://habr.com/ru/post/1208335/


All Articles