0

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<ResponseObject> 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();
DrGriff
  • 4,394
  • 9
  • 43
  • 92
  • 1
    Any reason why you can't use the first method for both, just omit the variable? – Lasse V. Karlsen May 01 '19 at 19:44
  • @Lasse Vågsæther Karlsen well, exactly. Why would Microsoft provide multiple ways to do the same thing - it just adds to confusion. Unless of course, there are subtle differences. – DrGriff May 01 '19 at 20:34
  • 1
    There are big differences. Don't use the `Task` constructor; that approach was never good. There's a variety of options [here](https://msdn.microsoft.com/en-us/magazine/mt238404.aspx). – Stephen Cleary May 02 '19 at 01:05
  • I wish Microsoft would deprecate their poor implementations and only leave the "really good ones". – DrGriff May 02 '19 at 08:29
  • There are an infinite number of ways you can combine the various building blocks and accomplish the same thing, the problem is that they are building blocks intended for different things. Deprecating/removing them to keep "the one true way" disregards all these different things and you would have a much worse situation. – Lasse V. Karlsen May 02 '19 at 08:44
  • Best practices, don't do it, have a sync method – johnny 5 May 02 '19 at 14:18

0 Answers0