Thread.Start Metodo

Definizione

Determina la pianificazione di un thread per l'esecuzione.

Overload

Nome Descrizione
Start()

Fa sì che il sistema operativo modifichi lo stato dell'istanza corrente in Running.

Start(Object)

Fa sì che il sistema operativo modifichi lo stato dell'istanza corrente in Runninge, facoltativamente, fornisca un oggetto contenente dati da utilizzare dal metodo eseguito dal thread.

Start()

Fa sì che il sistema operativo modifichi lo stato dell'istanza corrente in Running.

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

Eccezioni

Il thread è già stato avviato.

Memoria insufficiente per avviare questo thread.

Esempio

Nell'esempio seguente viene creato e avviato 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...

Commenti

Una volta che un thread è nello ThreadState.Running stato , il sistema operativo può pianificarlo per l'esecuzione. Il thread inizia l'esecuzione alla prima riga del metodo rappresentato dal ThreadStart delegato o ParameterizedThreadStart fornito al costruttore del thread. Si noti che la chiamata a Start non blocca il thread chiamante.

Note

Se questo overload viene usato con un thread creato usando un ParameterizedThreadStart delegato, null viene passato al metodo eseguito dal thread.

Una volta terminato il thread, non può essere riavviato con un'altra chiamata a Start.

Vedi anche

Si applica a

Start(Object)

Fa sì che il sistema operativo modifichi lo stato dell'istanza corrente in Runninge, facoltativamente, fornisca un oggetto contenente dati da utilizzare dal metodo eseguito dal thread.

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

Parametri

parameter
Object

Oggetto che contiene dati da utilizzare dal metodo eseguito dal thread.

Eccezioni

Il thread è già stato avviato.

Memoria insufficiente per avviare questo thread.

Questo thread è stato creato usando un ThreadStart delegato anziché un ParameterizedThreadStart delegato.

Esempio

Nell'esempio seguente viene creato un delegato con un ParameterizedThreadStart metodo statico e un metodo di istanza.

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

Commenti

Una volta che un thread è nello ThreadState.Running stato , il sistema operativo può pianificarlo per l'esecuzione. Il thread inizia l'esecuzione alla prima riga del metodo rappresentato dal ThreadStart delegato o ParameterizedThreadStart fornito al costruttore del thread. Si noti che la chiamata a Start non blocca il thread chiamante.

Una volta terminato il thread, non può essere riavviato con un'altra chiamata a Start.

Questo overload e il delegato semplificano il ParameterizedThreadStart passaggio di dati a una routine thread, ma la tecnica non è indipendente dai tipi perché qualsiasi oggetto può essere passato a questo overload. Un modo più affidabile per passare dati a una routine thread consiste nell'inserire sia la routine thread che i campi dati in un oggetto di lavoro. Per altre informazioni, vedere Creazione di thread e passaggio di dati all'ora di inizio.

Vedi anche

Si applica a