Task.Start 方法
定义
重载
| Start() |
启动 Task,并将它安排到当前的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the current TaskScheduler. |
| Start(TaskScheduler) |
启动 Task,并将它安排到指定的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the specified TaskScheduler. |
Start()
启动 Task,并将它安排到当前的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the current TaskScheduler.
public:
void Start();
public void Start ();
member this.Start : unit -> unit
Public Sub Start ()
例外
Task 并非要启动的有效状态。The Task is not in a valid state to be started. 它可能已启动、执行或取消,或者它可能是以不支持直接计划的方式创建的。It may have already been started, executed, or canceled, or it may have been created in a manner that doesn't support direct scheduling.
示例
下面的示例调用 Task(Action) 构造函数来实例化一个新 Task 的对象,该对象显示其任务 id 和托管线程 ID,然后执行一个循环。The following example calls the Task(Action) constructor to instantiate a new Task object that displays its task ID and managed thread ID and then executes a loop. 然后,它会调用 Start 方法来执行任务。It then calls the Start method to execute the task. 由于这是一个控制台应用程序,因此调用 Wait 方法是为了防止应用程序在任务完成执行之前终止。Since this is a console app, the call to the Wait method is necessary to prevent the app from terminating before the task finishes execution.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var t = new Task( () => { Console.WriteLine("Task {0} running on thread {1}",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
for (int ctr = 1; ctr <= 10; ctr++)
Console.WriteLine(" Iteration {0}", ctr); }
);
t.Start();
t.Wait();
}
}
// The example displays output like the following:
// Task 1 running on thread 3
// Iteration 1
// Iteration 2
// Iteration 3
// Iteration 4
// Iteration 5
// Iteration 6
// Iteration 7
// Iteration 8
// Iteration 9
// Iteration 10
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t As New Task(Sub()
Console.WriteLine("Task {0} running on thread {1}",
Task.CurrentId, Thread.CurrentThread.ManagedThreadId )
For ctr As Integer = 1 To 10
Console.WriteLine(" Iteration {0}", ctr)
Next
End Sub)
t.Start
t.Wait()
End Sub
End Module
' The example displays output like the following:
' Task 1 running on thread 3
' Iteration 1
' Iteration 2
' Iteration 3
' Iteration 4
' Iteration 5
' Iteration 6
' Iteration 7
' Iteration 8
' Iteration 9
' Iteration 10
注解
任务只能启动并运行一次。A task may be started and run only once. 如果尝试再次计划任务,将导致异常。Any attempts to schedule a task a second time will result in an exception.
Start用于执行已通过调用其中一个构造函数创建的任务 Task 。The Start is used to execute a task that has been created by calling one of the Task constructors. 通常情况下,在需要将任务的创建从其执行中分离出来(例如,在有条件地执行您创建的任务时)时,可以执行此操作。Typically, you do this when you need to separate the task's creation from its execution, such as when you conditionally execute tasks that you've created. 对于不需要将任务实例化与执行分离的更常见情况,我们建议调用 Task.Run 或方法的重载 TaskFactory.StartNew 。For the more common case in which you don't need to separate task instantiation from execution, we recommend that you call an overload of the Task.Run or TaskFactory.StartNew method.
有关处理任务操作引发的异常的信息,请参阅 异常处理。For information on handling exceptions thrown by task operations, see Exception Handling.
适用于
Start(TaskScheduler)
启动 Task,并将它安排到指定的 TaskScheduler 中执行。Starts the Task, scheduling it for execution to the specified TaskScheduler.
public:
void Start(System::Threading::Tasks::TaskScheduler ^ scheduler);
public void Start (System.Threading.Tasks.TaskScheduler scheduler);
member this.Start : System.Threading.Tasks.TaskScheduler -> unit
Public Sub Start (scheduler As TaskScheduler)
参数
- scheduler
- TaskScheduler
要与之关联并执行此任务的 TaskScheduler。The TaskScheduler with which to associate and execute this task.
例外
scheduler 参数为 null。The scheduler argument is null.
Task 并非要启动的有效状态。The Task is not in a valid state to be started. 它可能已启动、执行或取消,或者它可能是以不支持直接计划的方式创建的。It may have already been started, executed, or canceled, or it may have been created in a manner that doesn't support direct scheduling.
计划程序无法将此任务排入队列。The scheduler was unable to queue this task.
注解
一个任务只能启动并运行一次。A task may only be started and run only once. 如果尝试再次计划任务,将导致异常。Any attempts to schedule a task a second time will result in an exception.
有关处理任务操作引发的异常的信息,请参阅 异常处理。For information on handling exceptions thrown by task operations, see Exception Handling.