Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 2.33 KB

change-behavior-for-taskwaitall-methods-with-time-out-arguments.md

File metadata and controls

31 lines (20 loc) · 2.33 KB

Change in behavior for Task.WaitAll methods with time-out arguments

Details

xref:System.Threading.Tasks.Task.WaitAll%2A?displayProperty=nameWithType behavior was made more consistent in .NET Framework 4.5.In the .NET Framework 4, these methods behaved inconsistently. When the time-out expired, if one or more tasks were completed or canceled before the method call, the method threw an xref:System.AggregateException?displayProperty=fullName exception. When the time-out expired, if no tasks were completed or canceled before the method call, but one or more tasks entered these states after the method call, the method returned false.

In the .NET Framework 4.5, these method overloads now return false if any tasks are still running when the time-out interval expired, and they throw an xref:System.AggregateException?displayProperty=fullName exception only if an input task was cancelled (regardless of whether it was before or after the method call) and no other tasks are still running.

Suggestion

If an xref:System.AggregateException?displayProperty=fullName was being caught as a means of detecting a task that was cancelled prior to the xref:System.Threading.Tasks.Task.WaitAll%2A call being invoked, that code should instead do the same detection via the xref:System.Threading.Tasks.Task.IsCanceled%2A property (for example: .Any(t => t.IsCanceled)) since .NET Framework 4.6 will only throw in that case if all awaited tasks are completed prior to the timeout.

Value
Scope Minor
Version 4.5
Type Runtime

Affected APIs

  • xref:System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[],System.Int32)?displayProperty=nameWithType
  • xref:System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[],System.Int32,System.Threading.CancellationToken)?displayProperty=nameWithType
  • xref:System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[],System.TimeSpan)?displayProperty=nameWithType