none
Async/Await method take less time to execute RRS feed

  • Question

  • suppose i am doing heavy CPU bound job.

    public async Task<int> LongOperation()
    {
      await Task.Delay(500000); // (Probably should be longer...)
    
      // Return a type of "int", not "Task<int>"
      return 42;
    }

    if i run the above routine asynchronously does it take small time? if not then what is the objective of using async await keyword ? just to free the main thread and keep the ui responsive ? is it the objective of using async/await ?share the details and advantage of async await.


    • Edited by Sudip_inn Sunday, December 23, 2018 10:02 AM
    Sunday, December 23, 2018 10:02 AM

Answers

  • The purpose of async and await is to not block the calling thread, if it was called via a button click on  a windows application the UI will not freeze and user can interact with application.

    FROM official DOCS:

    You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. 

    The following docs explain well about how it works:

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


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by Ehsan Sajjad Sunday, December 23, 2018 10:46 AM
    • Marked as answer by Sudip_inn Sunday, December 23, 2018 5:27 PM
    Sunday, December 23, 2018 10:43 AM

All replies

  • The purpose of async and await is to not block the calling thread, if it was called via a button click on  a windows application the UI will not freeze and user can interact with application.

    FROM official DOCS:

    You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. 

    The following docs explain well about how it works:

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


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by Ehsan Sajjad Sunday, December 23, 2018 10:46 AM
    • Marked as answer by Sudip_inn Sunday, December 23, 2018 5:27 PM
    Sunday, December 23, 2018 10:43 AM
  • Thanks for you reply @Ehsan

    i post few more on same async/await. here is the link. can you please have a look at those link and share your answer & guideline.

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/fc023eca-0870-47a5-9575-d1af9bd1a1a1/running-two-routine-by-taskrun-at-same-time?forum=csharpgeneral
    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/58fbf608-255c-4410-80c9-fe91a380ef5d/how-to-update-ui-from-async-routine-without-begininvoke-or-invoke?forum=csharpgeneral
    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a27c0bfa-9505-49c8-b81b-b0d953c413f3/when-async-and-await-will-span-new-thread-?forum=csharpgeneral
    https://social.msdn.microsoft.com/Forum

    thanks

    Sunday, December 23, 2018 5:29 PM