Monday, August 26, 2019

c# - How to run async tasks synchronously (best practices)?

I've read so many posts on this, some of which are conflicting (possibly due to the age of the posts), so I just want to confirm my understanding of "best practices" as of .NET 4.8.


For an async method that returns Task then we should use:


ResponseObject response = myAsyncTask.GetAwaiter().GetResult();

And for an async method that returns Task, then we should use:


var t = new Task(async () => await myAsyncTask().ConfigureAwait(false));
t.RunSynchronously();

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...