PRG Chapter 5
PRG Chapter 5
Task provides the following powerful features over thread and threadpool.
1.Task allows you to return a result.
2.It gives better programmatical control to run and wait for a task.
3.It reduces the switching time among multiple threads.
4.It gives the ability to chain multiple tasks together and it can execute each task one
after the other by using ContinueWith().
5.It can create a parent/child relationship when one task is started from another task.
6.Task can cancel its execution by using cancellation tokens.
7.Task leaves the CLR from the overhead of creating more threads; instead it implicitly
uses the thread from threadpool.
8.Asynchronous implementation is easy in task, by using “async” and “await”
keywords.
9.Task waits for all of the provided Task objects to complete execution.
Methods and Properties of Task Class
Create and start a Task
Working with Tasks
Summary
A thread controls the flow of an executable program.
By default, a program has one thread called Main Thread.
Main Thread starts when control enters in the Main method
and it terminates when Main method returns.
Application has two kinds of threads: Background Thread
and Foreground Thread.
Background thread doesn’t hold the main thread to finish
its execution. If the main thread completes its execution it
will terminate the progam.
Foreground thread does hold the main thread to terminate
the progam until foreground completes its execution.
Threadpriority defines how much CPU time a thread will
have for execution.
Task doesn’t create new threads. Instead it efficiently
manages threads of a threadpool.
PRG521
Main | CH5
Heading | Subheading
The End
2 0 2 22 0C2T0U CTTr U
a i nTirnagi nSi nogl u St ioolnust i o|n sA l |l RAi gl l hRt si gRhet s eRr ev es edr v |e dc t u| t rcat iuntirnagi n. ai nc g
. z. a c . z a