Does Task.Wait(int) stop the task if the timeout elapses without the task finishing? Does Task.Wait(int) stop the task if the timeout elapses without the task finishing? multithreading multithreading

Does Task.Wait(int) stop the task if the timeout elapses without the task finishing?


Task.Wait() waits up to specified period for task completion and returns whether the task completed in the specified amount of time (or earlier) or not. The task itself is not modified and does not rely on waiting.

Read nice series: Parallelism in .NET, Parallelism in .NET – Part 10, Cancellation in PLINQ and the Parallel class by Reed Copsey

And: .NET 4 Cancellation Framework / Parallel Programming: Task Cancellation

Check following code:

var cts = new CancellationTokenSource();var newTask = Task.Factory.StartNew(state =>                           {                              var token = (CancellationToken)state;                              while (!token.IsCancellationRequested)                              {                              }                              token.ThrowIfCancellationRequested();                           }, cts.Token, cts.Token);if (!newTask.Wait(3000, cts.Token)) cts.Cancel();


If you want to cancel a Task, you should pass in a CancellationToken when you create the task. That will allow you to cancel the Task from the outside. You could tie cancellation to a timer if you want.

To create a Task with a Cancellation token see this example:

var tokenSource = new CancellationTokenSource();var token = tokenSource.Token;var t = Task.Factory.StartNew(() => {    // do some work    if (token.IsCancellationRequested) {        // Clean up as needed here ....    }    token.ThrowIfCancellationRequested();}, token);

To cancel the Task call Cancel() on the tokenSource.


The task is still running until you explicitly tell it to stop or your loop finishes (which will never happen).

You can check the return value of Wait to see this:

(from http://msdn.microsoft.com/en-us/library/dd235606.aspx)Return Value

Type: System.Booleantrue if the Task completed execution within the allotted time; otherwise, false.