Monday 26 August 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...