- What is a Task in C# ?
- Properties of Task class
- Different Methods in Task class
- How to create a Task in C#?
- Return a Value from Task
- Attaching Child Task to Parent Task
- Task Factory
What is a Task in C# ?
.net framework provides System.Threading.Tasks.Task class to let you create threads and run them asynchronously.
Queuing a work item to a thread pool is useful, but there is no way to know when the operation has finished and what the return value is.
So thats the reason Microsoft Introduced the concept of Task.
Task is an object that represents some work that should be done.
The Task can tell you if the work is completed and if the operation returns a result, the Task gives you the result.
Properties of Task Class:
Property | Description |
Exception | Returns any exceptions that caused the task to end early |
Status | Returns the Tasks status |
IsCancelled | Returns true if the Task was cancelled |
IsCompleted | Returns true if the task is completed successfully |
IsFaulted | Returns true if the task is stopped due to unhandled exception |
Factory | Provides acess to TaskFactory class. You can use that to create Tasks |
Methods in Task Class :
Methods | Purpose |
ConfigureAwait | You can use Await keyword for the Task to complete |
ContinueWith | Creates continuation tasks. |
Delay | Creates a task after specified amount of time |
Run | Creates a Task and queues it to start running |
RunSynchronously | Runs a task Synchronously |
Start | Starts a task |
Wait | Waits for the task to complete |
WaitAll | Waits until all tasks are completed |
WaitAny | Waits until any one of the tasks in a set completes |
WhenAll | Creates a Task that completes when all specified tasks are completed |
WhenAny | Creates a Task that completes when any specified tasks completes |
How to Create and start a Task in C# :
Let’s look at an example to understand it better.
In the above example, we have created a Task and started it.It waits till the Task is finished before exiting the application.
The wait method is similar to join method in Threads.
Output:
How to Return a Value from a Task?
The .NET Framework also has the Task class that you can use if you want a Task should return a value.
In the above example, The Task will return 100 and print 100 in console.
Output:
How to attach a child task to parent task?
The finalTask runs only after the parent Task is finished, and the parent Task finishes when all three children are finished
Task Factory :
In above example, you had to create three tasks all with same options.
So you can use TaskFactory to make it easier.You can create TaskFactory with certien configurations and then create Tasks.
You can use, wait method to wait on single task. Also WaitAll to wait for multiple tasks to finish.
So in the above example, all three Tasks are executed simultaneously and it is faster.
You can use WaitAny to wait until one of the task is completed.
You may also like:
- Parallel Programming in C#
- Background worker class
- Threads in C#
- Tasks in C#
- PLINQ in C#
- Cancelling Tasks in C#
- Call a method Asynchronously in C#
- async and await keyword in C#
- Asynchronous programming in C# using Async and Await keyword
- Split Work Among Processors in C# using Parallel.For()
- Use Reader-Writer Lock in C#
- Thread Synchronization
- Threadsafe Collection
http://www.csharpstar.com/tasks-csharp/
No comments:
Post a Comment