locked
What is the difference between task or async task RRS feed

  • Question

  • User828542847 posted

    i have seen people add async word with function signature and they use await keyword too but some time i saw people use async task too. so i like to know what is the difference between task or async task?

    when to use only async keyword with function signature and when to use async task keyword with function signature ?

    when we use async keyword with function signature then new worker thread is created to handle request or not?

    or new worker thread is created when we use task along with async keyword ?

    please clarify these usage. thanks

    Wednesday, September 13, 2017 10:25 AM

Answers

  • User475983607 posted

    A Task is an class that represents an asynchronous operation as explained in the reference documentation; https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

    The async keyword is a modifier that specifies a method is asynchronous.  It's syntax that makes writing asynchronous code easier for the developer.  You can learn all about it by reading the reference documentation; https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async

    when to use only async keyword with function signature and when to use async task keyword with function signature ?

    You'll get a syntax error if you have an async modifier without returning a Task or Task<T>.  Returning void is valid but not recommended except in a few use cases.

    when we use async keyword with function signature then new worker thread is created to handle request or not?

    I believe you are confusing asynchronous with multi-threading.  I'm assuming your questions are related to the Task-based Asynchronous Pattern (TAP)  which does the opposite. Rather than starting a new thread, threads are returned to the thread pool when the await key word is encountered.  When the async operation completes, a new thread from the thread pool executes the remainder of the logic.  That is why it is recommended you return Task rather than void and why you want async modifiers from the top level method to bottom. Remember this is syntax to make asynchronous programming easier.

    or new worker thread is created when we use task along with async keyword ?

    No, see above but there is nothing stopping you from spinning up a new thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 11:19 AM
  • User753101303 posted

    Hi,

    Could you show a sample? I'm not sure to understand the difference beetween :

    only async keyword with function signature
    async task keyword with function signature

    and AFAIK an async function must return Task or Task<T>. ah if you meant async void MyFunction() it should be your very last resort and you should use async Task MyFunction() instead whenever possible.

    The purpose of the async keyword is to tell the compiler to build a function so that :
    - you can exit from the function at the place of the await statement
    - you can go back into the function right after the previous await function

    This way you can write code that looks sequential but that relinquish control to the system while waiting for something and that the system can resume where it left once the wait is over (and more likely without using threads but by using specialized efficient mechanism waiting for IO completion for example).

    One of the best place for async stuff is https://blogs.msdn.microsoft.com/lucian/tag/async/ (I'm pretty sure where I saw once of the best async/await explanation ever even if I can't find right now this old post).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 11:32 AM

All replies

  • User475983607 posted

    A Task is an class that represents an asynchronous operation as explained in the reference documentation; https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

    The async keyword is a modifier that specifies a method is asynchronous.  It's syntax that makes writing asynchronous code easier for the developer.  You can learn all about it by reading the reference documentation; https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async

    when to use only async keyword with function signature and when to use async task keyword with function signature ?

    You'll get a syntax error if you have an async modifier without returning a Task or Task<T>.  Returning void is valid but not recommended except in a few use cases.

    when we use async keyword with function signature then new worker thread is created to handle request or not?

    I believe you are confusing asynchronous with multi-threading.  I'm assuming your questions are related to the Task-based Asynchronous Pattern (TAP)  which does the opposite. Rather than starting a new thread, threads are returned to the thread pool when the await key word is encountered.  When the async operation completes, a new thread from the thread pool executes the remainder of the logic.  That is why it is recommended you return Task rather than void and why you want async modifiers from the top level method to bottom. Remember this is syntax to make asynchronous programming easier.

    or new worker thread is created when we use task along with async keyword ?

    No, see above but there is nothing stopping you from spinning up a new thread.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 11:19 AM
  • User753101303 posted

    Hi,

    Could you show a sample? I'm not sure to understand the difference beetween :

    only async keyword with function signature
    async task keyword with function signature

    and AFAIK an async function must return Task or Task<T>. ah if you meant async void MyFunction() it should be your very last resort and you should use async Task MyFunction() instead whenever possible.

    The purpose of the async keyword is to tell the compiler to build a function so that :
    - you can exit from the function at the place of the await statement
    - you can go back into the function right after the previous await function

    This way you can write code that looks sequential but that relinquish control to the system while waiting for something and that the system can resume where it left once the wait is over (and more likely without using threads but by using specialized efficient mechanism waiting for IO completion for example).

    One of the best place for async stuff is https://blogs.msdn.microsoft.com/lucian/tag/async/ (I'm pretty sure where I saw once of the best async/await explanation ever even if I can't find right now this old post).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 11:32 AM
  • User828542847 posted

    what i understand from your explanation that async/await & task keyword does not create any separate thread.

    what is the objective of using async/await & task keyword ?

    tell me few scenario when we should use this and also tell me a situation where we should avoid these async/await & task keyword.

    thanks

    Tuesday, September 19, 2017 1:27 PM
  • User828542847 posted

    thanks for reply.

    what i understand from your explanation that async/await & task keyword does not create any separate thread.

    what is the objective of using async/await & task keyword ?

    tell me few scenario when we should use this and also tell me a situation where we should avoid these async/await & task keyword.

    thanks

    Tuesday, September 19, 2017 1:27 PM