C# – First steps with C# and .NET – part 013 .

The tutorial for today uses C# with .NET Framework 4.7.2 and is a short intro into the TAP programming area with definitions examples.
TAP is the acronym for Task-based Asynchronous Pattern.
In the .NET Framework 4.5, the C# compiler is capable of implementing the TAP and this implementing use compiler.
You can implement the TAP manually and must be sure to complete the resulting Task when the represented asynchronous operation completes.
When arguments should be verified outside of a compiler-generated asynchronous method then is a hybrid implementation.
TAP implementations should only be provided for workloads that involve I/O-bound operations with compute-bound and I/O-bound asynchronous operations as TAP methods.
Compute-bound tasks will end in a Canceled state, like CancellationToken
Examples:

Any method attributed to the async keyword is considered to be an asynchronous method.
The task-based asynchronous pattern (TAP) is based on the System.Threading.Tasks.Task and System.Threading.Tasks.Task types in the System.Threading.Tasks namespace.
When it completes, call the SetResult, SetException, or SetCanceled method, or the Try version of one of these methods.
The life cycle of this task is controlled by TaskCompletionSource use methods such as SetResult, SetException, SetCanceled, and TrySet variants.
The System.Threading.Tasks namespace includes several key methods for the Task class.
These several methods are Task.Run, Task.FromResult, Task.WhenAll, Task.WhenAny and Task.Delay.
Examples:

The System.Threading.Timer class already provides the ability to asynchronously invoke a delegate after a specified period of time.
With the .NET Framework 4.5, the Task.Delay method is provided for this purpose, and you can use it inside another asynchronous method.
The ability to completely represent an asynchronous operation and provide synchronous and asynchronous capabilities of potential combinator methods and types.
Examples:

The ability to build custom task-based to build custom data structures to be used in asynchronous scenarios.
Examples:

I will show you a simple working example with Task.Delay.
Open a simple console project named Task_TAP_001 and add this source code:

The result of this runnig source code is:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.