Task.Start 方法

定義

啟動 TaskStarts the Task.

多載

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 instance has been disposed.

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 物件,此物件會顯示其工作識別碼和 MANAGED 執行緒識別碼,然後執行迴圈。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用來執行透過呼叫其中一個函式所建立的工作 TaskThe 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.StartNewFor 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

要與這個工作產生關聯,並用於執行這個工作的 TaskSchedulerThe TaskScheduler with which to associate and execute this task.

例外狀況

scheduler 引數為 nullThe 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.

Task 執行個體已經過處置。The Task instance has been disposed.

排程器無法將這項工作排入佇列。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.

另請參閱

適用於