4

My understanding is that the following is bad practice since it blocks the thread:

Task t = Task.Run(() => MyMethod());
t.Wait();

Whilst the following does not block the thread:

await MyMethod();

However, to use await, one has to use the async keyword in the method signature, and return a Task rather than a void, or Task<T>.

However, in an n-Tier application, if you have a huge call-stack then do we have to have every calling method an async/Task as shown in the following simple example from LinqPad:

async void Main()
{
    int i = await GetNumberD();
    i.Dump();
}

async Task<int> GetNumberD()
{
    return await GetNumberC();
}
async Task<int> GetNumberC()
{
    return await GetNumberB();
}
async Task<int> GetNumberB()
{
    return await GetNumberA();
}
async Task<int> GetNumberA()
{
    await Task.Delay(TimeSpan.FromSeconds(1));
    return 7;
}
2
  • 2
    I don't think you understand how async methods work. Read through this introductory article msdn.microsoft.com/en-us/library/…
    – Spo1ler
    Mar 13, 2015 at 20:31
  • 1
    I think I'm quite clear on how the async method works. I'm just not clear on how this propagates up the call stack and the effect on the threads in each calling method. If you could respond to my comment on Otiel's response below then I'd be most grateful.
    – DrGriff
    Mar 13, 2015 at 21:09

2 Answers 2

2

Do we have to have every calling method an async/Task?

Yes you do, if the method called at the end of the stack is an async method.

Note that the documentation advise to end the name of the methods with the "Async" suffix:

async Task<int> GetNumberAsync()
5
  • Right - the method at the bottom of the stack IS an async method.
    – DrGriff
    Mar 13, 2015 at 21:03
  • So if that method at the bottom of the stack doesn't return immediately, then the calling thread gets returned to the thread pool until it completed. And if every method in the call stack has to be an async task then there will be quite a bit of thread re-allocation. What would seem efficient would be that Thread 33 starts off in Main, travels down through methods D, C, B and then when it hits the AWAIT, it goes back to the Tread pool. Thread 66 then returns up from A -> Main. If every method in stack has an await, then won't there be many more threads involved?
    – DrGriff
    Mar 13, 2015 at 21:07
  • @DrGriff: What you described is how async/await would work in this scenario (assuming there's no special context like a UI thread being captured) - the thread invoking Main is the same thread invoking Task.Delay, and the thread completing Task.Delay is the same thread resuming Main. I have an async intro blog post that you may find helpful. Mar 13, 2015 at 21:54
  • @StephenCleary This is important so please allow me to double check. Is the following a correct statement: If the leaf method async GetNumberA() in OP's call stacks is an async method, then the entire chain of methods in the call stacks have to be async too. And, consequently, if some of the GetNumberB() or GetNumberC() happen to be relied by my other method DoFoo() and DoBar(), then those DoFoo() and DoBar() need to be async too. Now I'm looking at my code base and see more than 50% of the methods are (forced to become) async methods. Is such situation still considered normal?
    – RayLuo
    Apr 7, 2017 at 6:47
  • 1
-1

You don't and please don't do it.

you can just await the Task.Run

await Task.Run(() => MyMethod());

async Task Main()
{
    int i = await Task.Run( () => GetNumberD());
    i.Dump();
}

int GetNumberD()
{
    return GetNumberC();
}
int GetNumberC()
{
    return GetNumberB();
}
int GetNumberB()
{
    return GetNumberA();
}
int GetNumberA()
{
    return 7;
}

I highly recommend you to read this article: http://blog.stephencleary.com/2013/10/taskrun-etiquette-and-proper-usage.html

EDIT This is not the right answer. As comments I misinterpreted with cpu bound task.

5
  • What if GetNumberA() is called in numerous other places? Should he write await Task.Run(() => GetNumberA()) everytime? Wouldn't it be simpler and clearer to define GetNumberA() as an async method?
    – Otiel
    Mar 13, 2015 at 20:54
  • it always depends, but you should not use Task.Run in the implementation. (I guess you can have a wrapper if you'd like). Read the stephencleary blog and you will understand why. Mar 13, 2015 at 20:56
  • 1
    The point of my question was that the Method GetNumberA() does something like download something from the internet asynchronously, and so had to have the signature: [ async Task<int> GetNumberA() ]. I only put in a Task.Delay(...) to mimic this.
    – DrGriff
    Mar 13, 2015 at 20:57
  • @DrGriff well, if that's the case, if you async because of scalability then yes, all methods should be async. think about it, what's the point of having an async method if the callers aren't async ? you must wait for the response. Mar 13, 2015 at 22:01
  • The main purpose would be for scalability so that we're not blocking a thread for an I/O bound task. Okay, that's what I suspected. My worry was whether that was the BEST practice - I wondered if one might end up capturing the state (locals) for all the methods in the call stack, rather than just those in the lowest method.
    – DrGriff
    Mar 13, 2015 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.