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:
1 2 | ...public Task<Bitmap> RenderAsync( ... , CancellationToken cancellationToken) ... ... TaskContinuationOptions.OnlyOnFaulted ... |
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
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
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:
1 2 3 4 5 | ... = await Task.Run(() => ... ... Task.FromResult( ... ... = await Task.WhenAll( ... ... = await Task.WhenAny( ... ... Task.Delay(1000) ... |
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:
1 2 3 4 | ... public static T RetryOnFault<T>( ... ... public static async Task<T> NeedOnlyOne( ... ... static IEnumerable<Task<T>> Interleaved<T>(IEnumerable<Task<T>> ... ... public static Task<T[]> WhenAllOrFirstException<T>( ... |
The ability to build custom task-based to build custom data structures to be used in asynchronous scenarios.
Examples:
1 2 | ... public class AsyncCache ... ... public class AsyncProducerConsumerCollection<T> ... |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; using System.Threading.Tasks; namespace Task_TAP_001 { class Program { static void Main(string[] args) { async Task<int> Count_For_Time_Async(int counter) { await Task.Delay(1000); counter++; // default return return counter; // use return with FromResult //return await Task.FromResult(counter); } // return a value using async/await Console.WriteLine(Count_For_Time_Async(10).GetAwaiter().GetResult()); } } } |
The result of this runnig source code is:
1 2 | Task_TAP_001.exe 11 |