How to Wait for Task in C# thread programming

By | February 3, 2017

In this article I will discuss the various ways by which we can wait for task completion in C#. There are many ways in which we can wait for a single task or multiple tasks.

Become an Expert in Programming and Web Development.View all courses!

Before going forward if you want to know more about tasks parallel library you can check the following articles.

Why to wait for Task?

Tasks are used for asynchronous programming. A long running operation is performed using the asynchronous call. But suppose we have issued a number of asynchronous calls and we want to perform some operation depending on the result of all these calls or may be the result of the any of the call which executed first.

Wait for Single Task

To wait for single task we can use the Wait method of the Task object. Check the below code.

The above code I am executing the LongRunningOperation which waits for some time duration. The task object which is being returned in turn is used to wait for the task to complete. Once complete I write the status of the task on the console. The status can be RanToCompletion, Cancelled, Faulted based on the operation in the method.

The other implicit way to wait for a single task is to check for the result. But in this case we should use the generic task. The long running method should call a method which has a return type. Check the C# code below

In the above code the result i.e. 2000 will be displayed after 2 seconds.

Wait for Multiple Tasks

There can be chance that we have to issue multiple tasks and wait for all of them. The below code handles this kind of scenario using Task.WaitAll. WaitAll will wait for all the tasks to complete.

Though I am calling the same method in all the tasks, but it can be different methods. The second parameter is a collection of Task object. I  have found this way of waiting the Task very helpful if we have to call a Web Api which exposes an async method. I can issue hundreds of call to the web api and wait for all of them to complete before going to further processing.

The other way to wait for the multiple task is using WhenAll  method of the Task class. This method will return a task which will complete when all the task objects in the array have been completed.

And if in case if you want to wait for only a single task to complete. You have to use WaitAny method of the Task class. The parameter of the WaitAny method is the same task collection.

This can be useful in scenario where we want to run any of the task. For example, if you a collection of some object and you want to search for any particular item in that collection. In that case you can device the list equal to the number of cores in the machine.  And perform a search operation on each part by sending each chunk part to the task.

Now suppose if I have quad core machine, in that case I will have four tasks running in parallel. And any task completing first should have the searched item.

In the above C# code I have four list which I am passing as argument to the LongRunningOperation method. And I am using WaitAny to wait for the first task to complete and get the task from the task list using the index.


In this article I have discussed many ways to wait for task and multiple task. I have also discussed the scenarios where these ways can be used to make most out of the tasks to improve the performance of the applications.