Thread.Start Méthode

Définition

Provoque la planification d’un thread pour l’exécution.

Surcharges

Nom Description
Start()

Provoque le changement de l’état de l’instance Runningactuelle par le système d’exploitation.

Start(Object)

Provoque le changement de l’état de l’instance Runningactuelle par le système d’exploitation et fournit éventuellement un objet contenant des données à utiliser par la méthode exécutée par le thread.

Start()

Provoque le changement de l’état de l’instance Runningactuelle par le système d’exploitation.

public:
 void Start();
public void Start();
member this.Start : unit -> unit
Public Sub Start ()

Exceptions

Le thread a déjà été démarré.

Il n’y a pas suffisamment de mémoire disponible pour démarrer ce thread.

Exemples

L’exemple suivant crée et démarre un thread.

using System;
using System.Threading;

public class ThreadWork
{
   public static void DoWork()
   {
      for(int i = 0; i<3;i++) {
         Console.WriteLine("Working thread...");
         Thread.Sleep(100);
      }
   }
}
class ThreadTest
{
   public static void Main()
   {
      Thread thread1 = new Thread(ThreadWork.DoWork);
      thread1.Start();
      for (int i = 0; i<3; i++) {
         Console.WriteLine("In main.");
         Thread.Sleep(100);
      }
   }
}
// The example displays output like the following:
//       In main.
//       Working thread...
//       In main.
//       Working thread...
//       In main.
//       Working thread...
open System.Threading

module ThreadWork = 
    let doWork () =
        for _ = 0 to 2 do 
            printfn "Working thread..."
            Thread.Sleep 100

let thread1 = Thread ThreadWork.doWork
thread1.Start()
for _ = 0 to 2 do 
    printfn "In main."
    Thread.Sleep 100

// The example displays output like the following:
//       In main.
//       Working thread...
//       In main.
//       Working thread...
//       In main.
//       Working thread...
Imports System.Threading

Public Class ThreadWork
   Public Shared Sub DoWork()
      Dim i As Integer
      For i = 0 To 2
         Console.WriteLine("Working thread...")
         Thread.Sleep(100)
      Next i
   End Sub
End Class

Class ThreadTest
   Public Shared Sub Main()
      Dim thread1 As New Thread(AddressOf ThreadWork.DoWork)
      thread1.Start()
      Dim i As Integer
      For i = 0 To 2
         Console.WriteLine("In main.")
         Thread.Sleep(100)
      Next
   End Sub
End Class
' The example displays output like the following:
'       In main.
'       Working thread...
'       In main.
'       Working thread...
'       In main.
'       Working thread...

Remarques

Une fois qu’un thread est dans l’état ThreadState.Running , le système d’exploitation peut le planifier pour l’exécution. Le thread commence à s’exécuter à la première ligne de la méthode représentée par le ou ParameterizedThreadStart le ThreadStart délégué fourni au constructeur de thread. Notez que l’appel à Start ne bloque pas le thread appelant.

Note

Si cette surcharge est utilisée avec un thread créé à l’aide d’un ParameterizedThreadStart délégué, null elle est transmise à la méthode exécutée par le thread.

Une fois le thread terminé, il ne peut pas être redémarré avec un autre appel à Start.

Voir aussi

S’applique à

Start(Object)

Provoque le changement de l’état de l’instance Runningactuelle par le système d’exploitation et fournit éventuellement un objet contenant des données à utiliser par la méthode exécutée par le thread.

public:
 void Start(System::Object ^ parameter);
public void Start(object parameter);
member this.Start : obj -> unit
Public Sub Start (parameter As Object)

Paramètres

parameter
Object

Objet qui contient des données à utiliser par la méthode exécutée par le thread.

Exceptions

Le thread a déjà été démarré.

Il n’y a pas suffisamment de mémoire disponible pour démarrer ce thread.

Ce thread a été créé à l’aide d’un ThreadStart délégué au lieu d’un ParameterizedThreadStart délégué.

Exemples

L’exemple suivant crée un ParameterizedThreadStart délégué avec une méthode statique et une méthode d’instance.

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        newThread.Start("The answer.");
    }
 
    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
open System.Threading

type Work() =
    static member DoWork(data: obj) =
        printfn $"Static thread procedure. Data='{data}'"

    member _.DoMoreWork(data: obj) =
        printfn $"Instance thread procedure. Data='{data}'"

// Start a thread that calls a parameterized static method.
let newThread = Thread(ParameterizedThreadStart Work.DoWork)
newThread.Start 42

// Start a thread that calls a parameterized instance method.
let w = Work()
let newThread2 = Thread(ParameterizedThreadStart w.DoMoreWork)
newThread.Start "The answer."

// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
Imports System.Threading

Public Class Work
    Shared Sub Main()
        ' Start a thread that calls a parameterized static method.
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start(42)

        ' Start a thread that calls a parameterized instance method.
        Dim w As New Work()
        newThread = New Thread(AddressOf w.DoMoreWork)
        newThread.Start("The answer.")
    End Sub
 
    Public Shared Sub DoWork(ByVal data As Object)
        Console.WriteLine("Static thread procedure. Data='{0}'",
                          data)
    End Sub

    Public Sub DoMoreWork(ByVal data As Object) 
        Console.WriteLine("Instance thread procedure. Data='{0}'",
                          data)
    End Sub
End Class
' This example displays output like the following:
'    Static thread procedure. Data='42'
'    Instance thread procedure. Data='The answer.'

Remarques

Une fois qu’un thread est dans l’état ThreadState.Running , le système d’exploitation peut le planifier pour l’exécution. Le thread commence à s’exécuter à la première ligne de la méthode représentée par le ou ParameterizedThreadStart le ThreadStart délégué fourni au constructeur de thread. Notez que l’appel à Start ne bloque pas le thread appelant.

Une fois le thread terminé, il ne peut pas être redémarré avec un autre appel à Start.

Cette surcharge et le ParameterizedThreadStart délégué facilitent la transmission de données à une procédure thread, mais la technique n’est pas sécurisée, car tout objet peut être passé à cette surcharge. Un moyen plus robuste de transmettre des données à une procédure thread consiste à placer à la fois la procédure thread et les champs de données dans un objet worker. Pour plus d’informations, consultez Création de threads et transmission de données au moment du début.

Voir aussi

S’applique à