Task.WaitAny 方法

定义

等待提供的任一 Task 对象完成执行过程。Waits for any of the provided Task objects to complete execution.

重载

WaitAny(Task[], Int32, CancellationToken)

等待提供的任何 Task 对象在指定的毫秒数内完成执行,或等到取消标记取消。Waits for any of the provided Task objects to complete execution within a specified number of milliseconds or until a cancellation token is cancelled.

WaitAny(Task[], TimeSpan)

等待任何提供的 Task 对象在指定的时间间隔内完成执行。Waits for any of the provided Task objects to complete execution within a specified time interval.

WaitAny(Task[], Int32)

等待任何提供的 Task 对象在指定的毫秒数内完成执行。Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.

WaitAny(Task[], CancellationToken)

等待提供的任何 Task 对象完成执行过程(除非取消等待)。Waits for any of the provided Task objects to complete execution unless the wait is cancelled.

WaitAny(Task[])

等待提供的任一 Task 对象完成执行过程。Waits for any of the provided Task objects to complete execution.

WaitAny(Task[], Int32, CancellationToken)

等待提供的任何 Task 对象在指定的毫秒数内完成执行,或等到取消标记取消。Waits for any of the provided Task objects to complete execution within a specified number of milliseconds or until a cancellation token is cancelled.

public:
 static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public static int WaitAny (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
static member WaitAny : System.Threading.Tasks.Task[] * int * System.Threading.CancellationToken -> int
Public Shared Function WaitAny (tasks As Task(), millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Integer

参数

tasks
Task[]

要等待的 Task 实例的数组。An array of Task instances on which to wait.

millisecondsTimeout
Int32

等待的毫秒数,或为 Infinite (-1),表示无限期等待。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

等待任务完成期间要观察的 CancellationTokenA CancellationToken to observe while waiting for a task to complete.

返回

已完成的任务在 tasks 数组参数中的索引,如果发生超时,则为 -1。The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

例外

已释放了 TaskThe Task has been disposed.

tasks 参数为 nullThe tasks argument is null.

millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

tasks 参数包含一个 null 元素。The tasks argument contains a null element.

已取消 cancellationTokenThe cancellationToken was canceled.

WaitAny(Task[], TimeSpan)

等待任何提供的 Task 对象在指定的时间间隔内完成执行。Waits for any of the provided Task objects to complete execution within a specified time interval.

public:
 static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, TimeSpan timeout);
public static int WaitAny (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
static member WaitAny : System.Threading.Tasks.Task[] * TimeSpan -> int
Public Shared Function WaitAny (tasks As Task(), timeout As TimeSpan) As Integer

参数

tasks
Task[]

要等待的 Task 实例的数组。An array of Task instances on which to wait.

timeout
TimeSpan

表示等待毫秒数的 TimeSpan,或表示 -1 毫秒(无限期等待)的 TimeSpanA TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

返回

已完成的任务在 tasks 数组参数中的索引,如果发生超时,则为 -1。The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

例外

已释放了 TaskThe Task has been disposed.

tasks 参数为 nullThe tasks argument is null.

TotalMilliseconds 参数的 timeout 属性是一个非 -1 的负数,而 -1 表示无限期超时。The TotalMilliseconds property of the timeout argument is a negative number other than -1, which represents an infinite time-out. -or- TotalMilliseconds 参数的 timeout 属性大于 MaxValueThe TotalMilliseconds property of the timeout argument is greater than MaxValue.

tasks 参数包含一个 null 元素。The tasks argument contains a null element.

WaitAny(Task[], Int32)

等待任何提供的 Task 对象在指定的毫秒数内完成执行。Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.

public:
 static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout);
public static int WaitAny (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
static member WaitAny : System.Threading.Tasks.Task[] * int -> int
Public Shared Function WaitAny (tasks As Task(), millisecondsTimeout As Integer) As Integer

参数

tasks
Task[]

要等待的 Task 实例的数组。An array of Task instances on which to wait.

millisecondsTimeout
Int32

等待的毫秒数,或为 Infinite (-1),表示无限期等待。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

返回

已完成的任务在 tasks 数组参数中的索引,如果发生超时,则为 -1。The index of the completed task in the tasks array argument, or -1 if the timeout occurred.

例外

已释放了 TaskThe Task has been disposed.

tasks 参数为 nullThe tasks argument is null.

millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

tasks 参数包含一个 null 元素。The tasks argument contains a null element.

WaitAny(Task[], CancellationToken)

等待提供的任何 Task 对象完成执行过程(除非取消等待)。Waits for any of the provided Task objects to complete execution unless the wait is cancelled.

public:
 static int WaitAny(cli::array <System::Threading::Tasks::Task ^> ^ tasks, System::Threading::CancellationToken cancellationToken);
public static int WaitAny (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
static member WaitAny : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> int
Public Shared Function WaitAny (tasks As Task(), cancellationToken As CancellationToken) As Integer

参数

tasks
Task[]

要等待的 Task 实例的数组。An array of Task instances on which to wait.

cancellationToken
CancellationToken

等待任务完成期间要观察的 CancellationTokenA CancellationToken to observe while waiting for a task to complete.

返回

已完成的任务在 tasks 数组参数中的索引。The index of the completed task in the tasks array argument.

例外

已释放了 TaskThe Task has been disposed.

tasks 参数为 nullThe tasks argument is null.

tasks 参数包含一个 null 元素。The tasks argument contains a null element.

已取消 cancellationTokenThe cancellationToken was canceled.

WaitAny(Task[])

等待提供的任一 Task 对象完成执行过程。Waits for any of the provided Task objects to complete execution.

public:
 static int WaitAny(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static int WaitAny (params System.Threading.Tasks.Task[] tasks);
static member WaitAny : System.Threading.Tasks.Task[] -> int
Public Shared Function WaitAny (ParamArray tasks As Task()) As Integer

参数

tasks
Task[]

要等待的 Task 实例的数组。An array of Task instances on which to wait.

返回

Task 数组中已完成的 tasks 对象的所有。The index of the completed Task object in the tasks array.

例外

已释放了 TaskThe Task has been disposed.

tasks 参数为 nullThe tasks argument is null.

tasks 参数包含一个 null 元素。The tasks argument contains a null element.

示例

下面的示例启动五个任务,每个任务都休眠至少50毫秒或最大1050毫秒。The following example launches five tasks, each of which sleeps for a minimum of 50 milliseconds or a maximum of 1,050 milliseconds. 然后 WaitAny 方法等待任何任务完成。The WaitAny method then waits for any of the tasks to complete. 该示例显示结束等待的任务的任务 ID 以及所有任务的当前状态。The example displays the task ID of the task that ended the wait, as well as the current status of all the tasks.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Task[] tasks = new Task[5];
      for (int ctr = 0; ctr <= 4; ctr++) {
         int factor = ctr;
         tasks[ctr] = Task.Run(() => Thread.Sleep(factor * 250 + 50));
      }
      int index = Task.WaitAny(tasks);
      Console.WriteLine("Wait ended because task #{0} completed.",
                        tasks[index].Id);
      Console.WriteLine("\nCurrent Status of Tasks:");
      foreach (var t in tasks)
         Console.WriteLine("   Task {0}: {1}", t.Id, t.Status);
   }
}
// The example displays output like the following:
//       Wait ended because task #1 completed.
//
//       Current Status of Tasks:
//          Task 1: RanToCompletion
//          Task 2: Running
//          Task 3: Running
//          Task 4: Running
//          Task 5: Running
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim tasks(4) As Task
      For ctr As Integer = 0 To 4
         Dim factor As Integer = ctr
         tasks(ctr) = Task.Run(Sub() Thread.Sleep(factor * 250 + 50))
      Next
      Dim index As Integer = Task.WaitAny(tasks)

      Console.WriteLine("Wait ended because task #{0} completed.",
                        tasks(index).Id)
      Console.WriteLine()
      Console.WriteLine("Current Status of Tasks:")
      For Each t In tasks
         Console.WriteLine("   Task {0}: {1}", t.Id, t.Status)
      Next
   End Sub
End Module
' The example displays output like the following:
'       Wait ended because task #1 completed.
'
'       Current Status of Tasks:
'          Task 1: RanToCompletion
'          Task 2: Running
'          Task 3: Running
'          Task 4: Running
'          Task 5: Running

适用于