Let's say I just wanted to make the following method asynchronous:
ResultType SynchronousCode(ParamType x)
{
return SomeLongRunningWebRequest(x);
}
What is the difference in how the following two code examples are executed / scheduled?
async Task<ResultType> AsynchronousCode(ParamType x)
{
return await Task.Run(() => SomeLongRunningWebRequest(x));
}
Compared with:
async Task<ResultType> AsynchronousCode(ParamType x)
{
await Task.Yield();
return SomeLongRunningWebRequest(x);
}
I understand that calling Task.Yield () ensures that the thread returns immediately to the caller, and that Task.Run () will definitely pay the code to run somewhere on ThreadPool, but is it effective to make both methods asynchronous? Assume for this question that we are in the standard SynchronizationContext.
source
share