Thread.Start Thread.Start Thread.Start Thread.Start Method

Definizione

Determina la pianificazione dell'esecuzione di un thread.Causes a thread to be scheduled for execution.

Overload

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

Determina il cambiamento da parte del sistema operativo dello stato dell'istanza corrente in Running.Causes the operating system to change the state of the current instance to Running.

Start(Object) Start(Object) Start(Object) Start(Object)

Determina il cambiamento da parte del sistema operativo dello stato dell'istanza corrente in Running e, facoltativamente, fornisce un oggetto contenente i dati che devono essere usati dal metodo eseguito dal thread.Causes the operating system to change the state of the current instance to Running, and optionally supplies an object containing data to be used by the method the thread executes.

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

Determina il cambiamento da parte del sistema operativo dello stato dell'istanza corrente in Running.Causes the operating system to change the state of the current instance to Running.

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

Eccezioni

Il thread è già stato avviato.The thread has already been started.

La memoria disponibile non è sufficiente per avviare il thread.There is not enough memory available to start this thread.

Esempi

Nell'esempio seguente viene creato e avviato un thread.The following example creates and starts a thread.

using namespace System;
using namespace System::Threading;

public ref class ThreadWork
{
public:
   static void DoWork()
   {
      for ( int i = 0; i < 3; i++ )
      {
         Console::WriteLine( "Working thread..." );
         Thread::Sleep( 100 );
      }
   }
};

int main()
{
   ThreadStart^ myThreadDelegate = gcnew ThreadStart(&ThreadWork::DoWork);
   Thread^ thread1 = gcnew Thread( myThreadDelegate );
   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...
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...
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

Quando un thread si trova nello stato ThreadState.Running, il sistema operativo può pianificarlo per l'esecuzione.Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution. Il thread inizia l'esecuzione in corrispondenza della prima riga del metodo rappresentato dal delegato ThreadStart o ParameterizedThreadStart fornito al costruttore del thread.The thread begins executing at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor. Si noti che la chiamata a Start non blocca il thread chiamante.Note that the call to Start does not block the calling thread.

Nota

Se questo overload viene usato con un thread creato usando un delegato ParameterizedThreadStart, null viene passato al metodo eseguito dal thread.If this overload is used with a thread created using a ParameterizedThreadStart delegate, null is passed to the method executed by the thread.

Una volta terminato, il thread non può essere riavviato con un'altra chiamata a Start.Once the thread terminates, it cannot be restarted with another call to Start.

Vedi anche

Start(Object) Start(Object) Start(Object) Start(Object)

Determina il cambiamento da parte del sistema operativo dello stato dell'istanza corrente in Running e, facoltativamente, fornisce un oggetto contenente i dati che devono essere usati dal metodo eseguito dal thread.Causes the operating system to change the state of the current instance to Running, and optionally supplies an object containing data to be used by the method the thread executes.

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

Oggetto contenente i dati che devono essere usati dal metodo eseguito dal thread.An object that contains data to be used by the method the thread executes.

Eccezioni

Il thread è già stato avviato.The thread has already been started.

La memoria disponibile non è sufficiente per avviare il thread.There is not enough memory available to start this thread.

Questo thread è stato creato usando un delegato di ThreadStart invece di uno di ParameterizedThreadStart.This thread was created using a ThreadStart delegate instead of a ParameterizedThreadStart delegate.

Esempi

Nell'esempio seguente viene creato un delegato ParameterizedThreadStart con un metodo statico e un metodo di istanza.The following example creates a ParameterizedThreadStart delegate with a static method and an instance method.

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
            // Start a thread that calls a parameterized static method.
            Thread^ newThread = gcnew
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
            newThread->Start(42);
              
            // Start a thread that calls a parameterized instance method.
            Work^ someWork = gcnew Work;
            newThread = gcnew Thread(
                        gcnew ParameterizedThreadStart(someWork,
                        &Work::DoMoreWork));
            newThread->Start("The answer.");
        }

        static void DoWork(Object^ data)
        {
            Console::WriteLine("Static thread procedure. Data='{0}'", 
                data);
        }

        void DoMoreWork(Object^ data)
        {
            Console::WriteLine("Instance thread procedure. Data='{0}'", 
                data);
        }
    };
}

//Entry point of example application
int main()
{
    SystemThreadingExample::Work^ samplework = 
        gcnew SystemThreadingExample::Work();
    samplework->StartThreads();
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
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.'
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

Quando un thread si trova nello stato ThreadState.Running, il sistema operativo può pianificarlo per l'esecuzione.Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution. Il thread inizia l'esecuzione in corrispondenza della prima riga del metodo rappresentato dal delegato ThreadStart o ParameterizedThreadStart fornito al costruttore del thread.The thread begins executing at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor. Si noti che la chiamata a Start non blocca il thread chiamante.Note that the call to Start does not block the calling thread.

Una volta terminato, il thread non può essere riavviato con un'altra chiamata a Start.Once the thread terminates, it cannot be restarted with another call to Start.

Questo overload e il delegato ParameterizedThreadStart facilitano il passaggio dei dati a una routine del thread, ma la tecnica non è indipendente dai tipi perché qualsiasi oggetto può essere passato a questo overload.This overload and the ParameterizedThreadStart delegate make it easy to pass data to a thread procedure, but the technique is not type safe because any object can be passed to this overload. Un modo più efficace per passare i dati a una routine di thread consiste nell'inserire sia la routine del thread che i campi dati in un oggetto di lavoro.A more robust way to pass data to a thread procedure is to put both the thread procedure and the data fields into a worker object. Per altre informazioni, vedere creazione di thread e passaggio di dati all'ora di inizio.For more information, see Creating Threads and Passing Data at Start Time.

Vedi anche

Si applica a