0

New to async and trying to understand when it makes sense to use it. We are going to have lots methods in webapi2 calling legacy webservices.

We have lots of low level dlls (Company.Dal.dll,Company.Biz.dll) etc.. that have methods that are not async

Question Does async has to be all the way really ? Is there any benefit of having an high level dll (all method async) calling low level dlls (dal,biz etc legacy code) where none of the method are async?

Is it there any benefit in having just the high level component to be async and the rest syncronous?

enter image description here

Many thanks for clarification Any good tutorials explaning this concept

2
  • Maybe it would help you if you better understood the benefits of async code and when it should be implemented. Then you can better decide if you should expose async or standard sync methods from your methods based on the operations it is performing and limitations of the libraries it uses. See Asynchronous Programming with async and await Commented May 14, 2016 at 7:09
  • @Igor thanks for your reply.I have already read that article but surely as a rule of thumb if you have an async method calling a sync method do i have any benefit is it worth it?That article does not answer my question Commented May 14, 2016 at 7:18

2 Answers 2

1

Using async only makes sense if you actually await something. If you don't, the async method will actually be completely synchronous (and you get a warning from the compiler about it).

In this case, async doesn't have any advantages, only disadvantages: the code is more complex and less efficient.

Sign up to request clarification or add additional context in comments.

2 Comments

,thanks for your reply.Let me try to understand what you are saying.Are you saying that if i am not doing async all the way there is no benefit? So if in dll1.method1Async calls dll2.Method1NotAsync there is no benefit whatsoever since there is block waiting for the call to finish from the not async method.
@developer9969 Exactly.
0

A thread can only do one thing at a time. If procedures keep your thread busy, there is no sense in making them async.

However if there are periods where the thread in your procedure has to wait for something else to finish, your thread might do something useful instead. In those circumstances async-await becomes useful.

Eric lippert once explained async-await with a restaurant metaphor (search on the page for async-await). If you have a cook who has to wait until the bread is toasted, this cook could do something else, like cooking an egg, and get back to the toaster when the "something else" is finished, or when has to wait for something, like await for the egg to be cooked.

In software the things where your thread typically will do nothing except waiting for something to finish is when reading / writing to disk, sending or receiving data over the network etc. Those are typically actions where you can find async versions as well as non-async versions of the procedure. See for instance classes like Stream, TextReader, WebClient, etc.

If your thread has to do a lot of calculations, it is not useful to make the function async, because there is no moment your thread will not do anything but wait, so your thread won't have time to do other things.

However, if your thread could do something useful while the calculations are done, consider letting another thread do those calculations while your thread is doing the other useful stuff:

private async Task<int> MyLengthyProcedure(...)
{
    Task<int> calculationTask = Task.Run( () => DoCalculations(...));
    // while one of the threads is doing the calculations,
    // your thread could do other useful things:
    int i = DoOtherCalculations();

    // or if there are calculations that could be performed
    // by separate threads simultaneously, start a second task
    Task<int> otherCalculationTask = Task.Run( () => DoEvenMoreCalculations(...));

    // now two threads are doing calculations. This thread is still free to
    // do other things

    // after a while you need the result of both calculations:
    await Task.WhenAll( new Task[] {calculationTask, otherCalculationTask});

    // the int return value of DoCalculations and DoOtherCalculations are in
    // the Result property of the task object:
    int j = calculationTask.Result;
    int k = otherCalculationTask.Result;
    return i + j + k;
;

1 Comment

,thanks for your time and reply.digesting what you are saying.however what I am trying to understand is different.If you have a dll that has async method and it calls a method eg "DAL" or "BLL" that is not async ,to my understanding I am not making an asyn call at all but I am blocking the thread once the call reaches my NOT ASYNC DLL METHOD.Is this correct?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.