If you want to do some work before the async operation finishes, just do that work before await
ing the returned Task
.
However, the primary purpose of async operations is to avoid consuming threads while waiting for non-blocking operations.
Question
I have an asynchronous method that does something peculiar (at least, looks peculiar to me):
public async ReturnType MethodNameHere()
{
var result = await DoSomethingAsync(); // this could take a while (dozens of seconds)!
// no other processing after awaiting the result of the asynchronous task
return result;
}
This method is consumed by another method, as follows (meant to run in an infinite loop):
private async void ProcessSomething()
{
// some console printing
var returnType = await MethodNameHere();
// do some _actual_ work here, process stuff, etc
ProcessSomething(); // call the same method again
}
Now this task is running via Task.Factory.StartNew(ProcessSomething, TaskCreationOptions.LongRunning)
, which, along with other tasks that do similar work, are combined with Task.Factory.ContinueWhenAll
.
Question
Is it correct to assume that, if the scheduler ends up putting each task on the same thread as one another, the tasks will not block each other?
If that is the case, is there even any benefit to calling the asynchronous version of DoSomething
if it is to be running "alone" in a Task
?
Solution
If you want to do some work before the async operation finishes, just do that work before await
ing the returned Task
.
However, the primary purpose of async operations is to avoid consuming threads while waiting for non-blocking operations.