TPL – C# Task Parallel Library

advantages:
1. heavy actions do not stuck the UI due to the using of another thread and maybe even a different core
a good example for this phenomenon is when working with winForms and perform an heavy task – the UI is not responsive

2. an abstraction that built on top of the Threads (using ThreadPool backstage) and manages the threadPool for better performance

// create new task without starting it – use start() method to start the task
var task = new Task([Action])
task.start();

// wait for the task to complete
task.Wait();

// once the prevoius task is done – continue to another task – use prevTask.Result in order to get the return value of the previous task
task.ContinueWith([Action(prevTask)]);

// choose the thread context which will execute this task – in this example we refer to the main thread
TaskScheduler.FromCurrentSynchronizationContext();

// task that returns T value
Task<T>

// holds the return value of the task – also, waits for the task to complete
task.Result

// async is a method decorator – it only inform the compiler that this method may include a wait clause within it
// await is the clause uses to wait for a task to complete
async … await

// in addition, we can use some independend code while waiting to the task to complete – just need to set the code between the task and the await
var task = MyMethodAsync();
code here …
var result = await task;
code here …

// BlockingCollection
BlockingCollection uses ConcurrentQueue backstage which serve as a Thread safe Queue, but with an addon of the max queue size allowed
when this max queue is reached – the queue will go to sleep – this behaviour handle a memory leaks
where the producers continuing to produce and flood the queue while there are no cunsumers to cunsume from it

// recommendation: we should add the ‘Async’ suffix to all of our async methods
private async void ExecuteAsync(){ … }

result:
the main thread will NOT wait the task to complete its work and therefore it will start working and print the ‘END’
when the task will finish its background proccess – it will print ‘TASK COMPLETED’

result:
task.Result holds the return value of the task
note! when calling to task Result property – it waits for the task to complete its work so we do NOT require the task.wait clause

——-

- async and await -

steps:
1. create a method that return a task
2. create an async method that execute the method from chapter 1 using an await syntax
note! Must be decorated as async in its signature
3. call the async method from chapter 2

result:
when using async and await we have to create a method that return a Task – the await claue will wait the task to finish
and only then – will continue the code comes after it
- async and await important note -

the main thread continue working and not paused by the ExecuteAsync method!
the reason is that await replaced br the compiler to Task and ContinueWith whereas the await content will be the task and the content after the await
will be the ContinueWith.

e.g:
the original code below translated into the translation code beneath by the compiler:

result:
the 1st loop will exexute 10 instances of myAction one after the other in a single thread (whole proccess will take 20 sec)
the 2sn loop will perform 10 instances of myAction in parallel (whole proccess will take 6 sec!!)
*
——-