3

I have seen examples like the followings to call asynchronous method from synchronous method

Task.Run(async () => await MyAsyncMethod()).ConfigureAwait(false).GetAwaiter().GetResult();

and

Task.Run(() => MyAsyncMethod()).GetAwaiter().GetResult();

I know the ConfigreAwait is needed only for the await call to synchronize back to its context. My question is why and when to use the async and await in the Task.Run (1st sample above).

Just find out the similar question was asked at Aync/Await action within Task.Run()

kklo
  • 661
  • 1
  • 8
  • 14
  • Ugh, all this effort. Why not just make the calling method async and have it return a Task? – mason Oct 24 '18 at 01:15
  • Task.Run fo CPU bound. No need for Task.Run for IO bound code. Your code e.g. shows how not to use Task.Run – FCin Oct 24 '18 at 04:47
  • 1
    @pere57 Because it returns Task, which means it is either CPU bound or CPU and IO bound. If latter is true then it is even a bigger problem, because they should be separated into different methods. Also, this isn't an answer but a comment, so my intention wasn't to answer the question, but give general tip. – FCin Oct 24 '18 at 05:32

0 Answers0