Skip to content

Different behavior of Async Await with a SynchronizationContext ( UI ) and without it in C#

Notifications You must be signed in to change notification settings

amitsbaghel/AsyncAwaitCSharpSynchronizationContext

Repository files navigation

AsyncAwaitCSharpSynchronizationContext

Different behavior of async await with/wihout a SynchronizationContext ( UI ) in C#

With SynchronizationContext ( e.g. in WPF/WinForms app)

Thread Id initially: 1
Thread Id inside DownloadString call before calling GetAsync: 1
Thread Id inside DownloadString call after calling GetAsync: 1
Thread Id after DownloadString call in Main: 1
Thread Id inside FirstAsyncCall call before Task Delay: 1
Thread Id inside FirstAsyncCall call after Task Delay: 1
Thread Id after all calls in Main: 1
-------------------------------------------------------------------------------

Without SynchronizationContext ( e.g. in a Console app)

Thread Id initially: 1
Thread Id inside DownloadString call before calling GetAsync: 1
Thread Id inside DownloadString call after calling GetAsync: 9
Thread Id after DownloadString call in Main: 9
Thread Id inside FirstAsyncCall call before Task Delay: 9
Thread Id inside FirstAsyncCall call after Task Delay: 5
Thread Id after all calls in Main: 5

About

Different behavior of Async Await with a SynchronizationContext ( UI ) and without it in C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages