Wait Method (TimeSpan)
Collapse the table of content
Expand the table of content

Task.Wait Method (TimeSpan)

 

Waits for the Task to complete execution within a specified time interval.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

member Wait : 
        timeout:TimeSpan -> bool

Parameters

timeout
Type: System.TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Return Value

Type: System.Boolean

true if the Task completed execution within the allotted time; otherwise, false.

Exception Condition
ObjectDisposedException

The Task has been disposed.

ArgumentOutOfRangeException

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out.

-or-

timeout is greater than MaxValue.

AggregateException

The task was canceled. The AggregateException.InnerExceptions collection contains a TaskCanceledException object.

-or-

An exception was thrown during the execution of the task. The AggregateException.InnerExceptions collection contains information about the exception or exceptions.

Wait(TimeSpan) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

  • The task completes successfully.

  • The task itself is canceled or throws an exception. In this case, you handle an AggregateException exception. The AggregateException.InnerExceptions property contains details about the exception or exceptions.

  • The interval defined by timeout elapses. In this case, the current thread resumes execution and the method returns false.

The following example starts a task that generates five million random integers between 0 and 100 and computes their mean. The example uses the Wait(TimeSpan) method to wait for the application to complete within 150 milliseconds. If the application completes normally, the task displays the sum and mean of the random numbers that it has generated. If the timeout interval has elapsed, the example displays a message before it terminates.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft