Wednesday 13 September 2017

What is a Task in C# ?

  • 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:

PropertyDescription
ExceptionReturns any exceptions that caused the task to end early
StatusReturns the Tasks status
IsCancelledReturns true if the Task was cancelled
IsCompletedReturns true if the task is completed successfully
IsFaultedReturns true if the task is stopped due to unhandled exception
FactoryProvides acess to TaskFactory class. You can use that to create Tasks

Methods in Task Class :

MethodsPurpose
ConfigureAwaitYou can use Await keyword for the Task to complete
ContinueWithCreates continuation tasks.
DelayCreates a task after specified amount of time
RunCreates a Task and queues it to start running
RunSynchronouslyRuns a task Synchronously
StartStarts a task
WaitWaits for the task to complete
WaitAllWaits until all tasks are completed
WaitAnyWaits until any one of the tasks in a set completes
WhenAllCreates a Task that completes when all specified tasks are completed
WhenAnyCreates 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:

It will print “Hi” 50 times to the console.
Task in C#

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:

Task returns value in C#

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:
http://www.csharpstar.com/tasks-csharp/

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0