Task.Start Task.Start Task.Start Task.Start Method

定義

啟動 TaskStarts the Task.

多載

Start() Start() Start() Start()

啟動 Task,並排定它在目前的 TaskScheduler 執行。Starts the Task, scheduling it for execution to the current TaskScheduler.

Start(TaskScheduler) Start(TaskScheduler) Start(TaskScheduler) Start(TaskScheduler)

啟動 Task,並排定它在指定的 TaskScheduler 執行。Starts the Task, scheduling it for execution to the specified TaskScheduler.

Start() Start() Start() 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 物件,它會顯示其工作識別碼和受控執行緒識別碼,然後執行迴圈。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.

@No__t-0 是用來執行已藉由呼叫其中一個 @no__t 1 的函式所建立的工作。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.RunTaskFactory.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) Start(TaskScheduler) Start(TaskScheduler) 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 TaskScheduler 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.

另請參閱

適用於