You can prevent this behavior by appending ConfigureAwait(false) to your call: await DoSomethingAsync().ConfigureAwait(false) If you don't need to restore that context later, then it's just wasted resources. This can have a not insignificant performance impact. When you await an asynchronous operation, the context of the calling code is passed along by default. Then AsyncHelper.RunSync(() => DoAsyncStuff()) Public static TResult RunSync(Func> func) Private static readonly TaskFactory _taskFactory = new It appears is various namespaces, but always as an internal, so you can't just use it directly from the framework. Run async method as syncįor this one we have a helper class borrowed from Microsoft. In a web application context, this is pretty much pointless, since every thread comes from the same pool for serving requests that the main (request) thread came from and the response won't be returned until everything is done anyways. This is really only useful to keep from blocking the UI thread with desktop/mobile applications. It's still blocking, but it runs on a background thread. Run sync method as "async" Task.Run(() => DoSyncStuff()) This is as much a reference for myself as anyone else, but I figured it would probably useful for others as well. The following are few tips and tricks for working with async in C#. C# Async Tips & Tricks Home Subscribe C# Async Tips & Tricks 04 October 2017 on async, csharp, c#
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |