What is the use of Task-based asynchronous pattern (TAP)?

Lalith 161 Reputation points
2022-01-11T07:24:08.693+00:00

A Task is work that runs asynchronously on a worker thread of thread pool, which is a back ground thread. Each Task will be assigned with a separate thread from the thread pool provided the availability of threads from thread pool. now using these Tasks you can consume the thread from the thread pool without you having to maintain threads using Thread Class. So, When a task is already asynchronous what's the point of TAP? Please help me understand this, thank you.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,127 questions
{count} votes

Accepted answer
  1. Sreeju Nair 12,456 Reputation points
    2022-01-11T09:32:47.207+00:00

    In .Net, therre are three patterns for performing asynchronous operations They are

    1. Task based Asnynchronous Pattern (TAP)
    2. Event based Asynchronous Pattern (EAP)
    3. Asynchronous Programming Model (APM)

    You can refer each of these in the below URL.
    https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/

    So here the classification is for the asynchronous patterns and Task based is one among other choices. Hope this is clear. You can read more about TAP below.

    https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap

    Hope this helps

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 28,876 Reputation points
    2022-01-11T12:56:30.507+00:00

    C# is a top level language that calls system APIs. These system APIs are asynchronous. C# code is not asynchronous unless you use the TAP pattern or one of the pervious asynchronous patterns. When synchronous code calls a asynchronous API, the synchronous logic waits for the asynchronous API to complete before executing the next line of code. This can cause a bottleneck.

    When asynchronous C# code calls a asynchronous API, the C# thread is returned to the thread pool rather than waiting for the asynchronous API to complete. When the asynchronous API returns a new thread executes the remaining code. This is a better use of resources.

    0 comments No comments

  2. Xingyu Zhao-MSFT 5,366 Reputation points
    2022-01-12T02:59:19.35+00:00

    Hi @Lalith ,
    The Task-based Asynchronous Pattern uses Task, Task and ValueTask for asynchronous operations. It enables developers to define asynchronous functions within a single method definition, instead of having "begin" and "end" function pairs or separate callbacks. This makes coding the asynchronous function very intuitive and clear in C# 4.5 (version next). It also integrates a mechanism for progress reporting and a cancellation framework to complete the loop.
    The following are the benefits of using Task which comes from TAP.

    1. you can get the status of Task.
    2. you can use cancelation mechanism.
    3. you can get progress notifications of each asynchronous operation
    4. you can combine multiple Tasks in several ways(Task.ContinueWith, Task.WhenAll, Task.WhenAny)

    Hope them could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.