Επεξεργασία

Κοινή χρήση μέσω


Task.Start Method

Definition

Starts the Task.

Overloads

Start()

Starts the Task, scheduling it for execution to the current TaskScheduler.

Start(TaskScheduler)

Starts the Task, scheduling it for execution to the specified TaskScheduler.

Start()

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

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 ()

Exceptions

The Task instance has been disposed.

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.

Examples

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. It then calls the Start method to execute the task. 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
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

Remarks

A task may be started and run only once. Any attempts to schedule a task a second time will result in an exception.

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. 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.

See also

Applies to

Start(TaskScheduler)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

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)

Parameters

scheduler
TaskScheduler

The TaskScheduler with which to associate and execute this task.

Exceptions

The scheduler argument is null.

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

The scheduler was unable to queue this task.

Remarks

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.

See also

Applies to