Can ConfigureAwait(false) in a library lose the synchronization context for the calling application?

后端 未结 3 2341
故里飘歌
故里飘歌 2021-02-20 09:54

I\'ve read the advice many times from people smarter than me, and it has few caveats: Always use ConfigureAwait(false) inside library code. So I\'m

3条回答
  •  死守一世寂寞
    2021-02-20 10:23

    Example from http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx

    ... logically you can think of the following code:

    await FooAsync();
    RestOfMethod();
    

    as being similar in nature to this:

    var t = FooAsync();
    var currentContext = SynchronizationContext.Current;
    t.ContinueWith(delegate
    {
        if (currentContext == null)
            RestOfMethod();
        else
            currentContext.Post(delegate { RestOfMethod(); }, null);
    }, TaskScheduler.Current);
    

    which means you should have your context back after the await myLib.DoThingAync(); call.

提交回复
热议问题