locked
asynchronous programming RRS feed

  • Question

  • User585649674 posted

    If i have code like below, when does the actual execution of method "GetNumber()" starts? Is it at line 1 or line 4 ? I read that only a task object is created in line 1 and the method starts executing only from line 4 ? if so should i call "intTask.Start()" ?

    {
    Task<int> intTask = GetNumber() //line 1
    Task<string> stringTask = GetString() //line2
    Task<double> doubleTask = GetDouble() //line3
    int intOutput = await intTask //line4 
    }
    

    Saturday, May 9, 2020 9:43 AM

Answers

  • User475983607 posted

    Lets assume GetNumber makes an third party http call and returns a number.

    I want to perform two independent http calls in parallel .

    If i have two codes which one runs fasters

    /// first
    Task<int> firstTask = GetNumber();
    Task<int> secondTask = GetNumber();
    int output1 = await firstTask;
    int output2 = await secondTask;
    
    
    /// second
    int output1 = await GetNumber();
    int output2 = await GetNumber();

    The only way to know which async response returns first is to time the responses.   Assuming GetNumber() is deterministic, it is reasonable to assume that most of the time the first method will return before the second but there is no guarantee.  For example, the first HTTP request might take a longer path through the network. 

    But In Web MVC / API appilcation each controller action is a UI thread. Untill the server executes and returns the browser is not blocked. What is the advantage of async.

    What is the purpose of configure await.

    Any documentation or blog url is most welcome

    Not exactly.  Honestly, the official async/await documentation is very very good.  I recommend stating with the "manufacturer" docs.   Keep in mind there is a a lot to read and it might take a while to fully understand.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

    Another avenue is looking into how computer hardware works because everything inside you compute is asynchronous and accessible through APIs.   The concept of asynchronous programming is not new.  It been around since the beginning of time but it has always been difficult to code and organize.  The async/await pattern make asynchronous programming seem synchronous.  Behind the scenes the compiler is spiting up the code to work with the underlying asynchronous APIs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 3:21 PM
  • User753101303 posted

    In short as pointed with the material posted by mgebhard, the pattern is to get each task as you are doing (which starts them) and then await Task.WhenAll on all them to get back control when they are all completed.

    The primary purpose is scalability ie async APIs are written using an efficient mechanism that keeps the CPU free for doing other stuff rather than waiting for I/O completion. In addition to the documentation you can have a look at https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async

    Edit: it is not always easy to figure out what is really needed when someone post code and ask some detail about how the code works (and without even telling if it works fine or not). When you have a general question, just ask directly this question.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 4:21 PM

All replies

  • User753101303 posted

    Hi,

    What have you done in GetNumber? GetNumberAsync (the Async suffix is a suggested convention) should return an already running task that you could await later.

    For a start (and in most cases), I expect you'll consume Async methods. Avoid trying to turn all your methods in Async methods if it doesn't lend itself to being async (ie you create a thread in GetNumber ?)

    Saturday, May 9, 2020 11:25 AM
  • User585649674 posted

    What have you done in GetNumber? GetNumberAsync (the Async suffix is a suggested convention) should return an already running task that you could await later.

    Lets assume GetNumber makes an third party http call and returns a number.

    I want to perform two independent http calls in parallel .

    If i have two codes which one runs fasters

    /// first
    Task<int> firstTask = GetNumber();
    Task<int> secondTask = GetNumber();
    int output1 = await firstTask;
    int output2 = await secondTask;
    
    
    /// second
    int output1 = await GetNumber();
    int output2 = await GetNumber();

    Most of the documentation are talking about UI Thread. I understand the concept of forms being unresponsive because UI thread is blocked due to one button click and user cannot click another button in  Windows forms / WPF.

    But In Web MVC / API appilcation each controller action is a UI thread. Untill the server executes and returns the browser is not blocked. What is the advantage of async.

    What is the purpose of configure await.

    Any documentation or blog url is most welcome

    Sunday, May 17, 2020 2:42 PM
  • User475983607 posted

    Lets assume GetNumber makes an third party http call and returns a number.

    I want to perform two independent http calls in parallel .

    If i have two codes which one runs fasters

    /// first
    Task<int> firstTask = GetNumber();
    Task<int> secondTask = GetNumber();
    int output1 = await firstTask;
    int output2 = await secondTask;
    
    
    /// second
    int output1 = await GetNumber();
    int output2 = await GetNumber();

    The only way to know which async response returns first is to time the responses.   Assuming GetNumber() is deterministic, it is reasonable to assume that most of the time the first method will return before the second but there is no guarantee.  For example, the first HTTP request might take a longer path through the network. 

    But In Web MVC / API appilcation each controller action is a UI thread. Untill the server executes and returns the browser is not blocked. What is the advantage of async.

    What is the purpose of configure await.

    Any documentation or blog url is most welcome

    Not exactly.  Honestly, the official async/await documentation is very very good.  I recommend stating with the "manufacturer" docs.   Keep in mind there is a a lot to read and it might take a while to fully understand.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

    Another avenue is looking into how computer hardware works because everything inside you compute is asynchronous and accessible through APIs.   The concept of asynchronous programming is not new.  It been around since the beginning of time but it has always been difficult to code and organize.  The async/await pattern make asynchronous programming seem synchronous.  Behind the scenes the compiler is spiting up the code to work with the underlying asynchronous APIs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 3:21 PM
  • User753101303 posted

    In short as pointed with the material posted by mgebhard, the pattern is to get each task as you are doing (which starts them) and then await Task.WhenAll on all them to get back control when they are all completed.

    The primary purpose is scalability ie async APIs are written using an efficient mechanism that keeps the CPU free for doing other stuff rather than waiting for I/O completion. In addition to the documentation you can have a look at https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async

    Edit: it is not always easy to figure out what is really needed when someone post code and ask some detail about how the code works (and without even telling if it works fine or not). When you have a general question, just ask directly this question.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 4:21 PM