Task.Start 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
启动 。Task
重载
| 名称 | 说明 |
|---|---|
| Start() |
Task启动 ,计划执行到当前TaskScheduler。 |
| Start(TaskScheduler) |
Task启动 ,计划将其执行到指定的 TaskScheduler。 |
Start()
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
Task启动 ,计划执行到当前TaskScheduler。
public:
void Start();
public void Start();
member this.Start : unit -> unit
Public Sub Start ()
例外
实例 Task 已释放。
未 Task 处于要启动的有效状态。 它可能已启动、执行或取消,或者可能以不支持直接计划的方式创建。
示例
以下示例调用 Task(Action) 构造函数来实例化显示其任务 ID 和托管线程 ID 的新 Task 对象,然后执行循环。 然后,它会调用 Start 该方法来执行任务。 由于这是控制台应用, Wait 因此在任务完成执行之前,必须调用该方法以防止应用终止。
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
open System.Threading
open System.Threading.Tasks
let t =
new Task(fun () ->
printfn $"Task {Task.CurrentId} running on thread {Thread.CurrentThread.ManagedThreadId}"
for i = 1 to 10 do
printfn $" Iteration {i}")
t.Start()
t.Wait() |> ignore
// 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
注解
任务只能启动并运行一次。 第二次计划任务的任何尝试都将导致异常。
用于 Start 执行通过调用其中 Task 一个构造函数创建的任务。 通常,当需要将任务的创建与其执行区分开时(例如,在有条件地执行已创建的任务时),可以执行此操作。 对于不需要将任务实例化与执行分开的更常见情况,建议调用或Task.Run方法的TaskFactory.StartNew重载。
有关处理任务操作引发的异常的信息,请参阅 异常处理。
另请参阅
适用于
Start(TaskScheduler)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
Task启动 ,计划将其执行到指定的 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 与之关联和执行此任务的项。
例外
参数 scheduler 为 null.
未 Task 处于要启动的有效状态。 它可能已启动、执行或取消,或者可能以不支持直接计划的方式创建。
实例 Task 已释放。
计划程序无法对此任务进行排队。
注解
任务只能启动并运行一次。 第二次计划任务的任何尝试都将导致异常。
有关处理任务操作引发的异常的信息,请参阅 异常处理。