Thread.Start 方法

定義

使執行緒進行執行排程。

多載

Start()

造成作業系統將目前執行個體的狀態變更為 Running

Start(Object)

使作業系統將目前執行個體的狀態改成 Running,並選擇性地提供物件,在物件中包含執行緒執行之方法所要使用的資料。

Start()

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

造成作業系統將目前執行個體的狀態變更為 Running

public:
 void Start();
public void Start ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Start ();
member this.Start : unit -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Start : unit -> unit
Public Sub Start ()
屬性

例外狀況

已經啟動執行緒。

沒有足夠的記憶體可用來啟動這個執行緒。

範例

下列範例會建立並啟動執行緒。

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

備註

一旦執行緒處於 ThreadState.Running 狀態,作業系統就可以將它排程為執行。 執行緒會在提供給執行緒建構函式的 或 ParameterizedThreadStart 委派所 ThreadStart 表示的方法第一行開始執行。 請注意,呼叫 Start 不會封鎖呼叫執行緒。

注意

如果此多載與使用 ParameterizedThreadStart 委派建立的執行緒搭配使用, null 則會傳遞至執行緒所執行的方法。

執行緒終止之後,就無法以另一個 呼叫 Start 重新開機。

另請參閱

適用於

Start(Object)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

使作業系統將目前執行個體的狀態改成 Running,並選擇性地提供物件,在物件中包含執行緒執行之方法所要使用的資料。

public:
 void Start(System::Object ^ parameter);
public void Start (object? parameter);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Start (object? parameter);
public void Start (object parameter);
member this.Start : obj -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Start : obj -> unit
Public Sub Start (parameter As Object)

參數

parameter
Object

物件,包含執行緒執行之方法所要使用的資料。

屬性

例外狀況

已經啟動執行緒。

沒有足夠的記憶體可用來啟動這個執行緒。

這個執行緒是使用 ThreadStart 委派建立,而非 ParameterizedThreadStart 委派。

範例

下列範例會建立具有 ParameterizedThreadStart 靜態方法和實例方法的委派。

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

備註

一旦執行緒處於 ThreadState.Running 狀態,作業系統就可以將它排程為執行。 執行緒會在提供給執行緒建構函式的 或 ParameterizedThreadStart 委派所 ThreadStart 表示的方法第一行開始執行。 請注意,呼叫 Start 不會封鎖呼叫執行緒。

執行緒終止之後,就無法以另一個 呼叫 Start 重新開機。

此多載和 ParameterizedThreadStart 委派可讓您輕鬆地將資料傳遞至執行緒程式,但技術的類型並不安全,因為任何物件都可以傳遞至這個多載。 將資料傳遞至執行緒程式的更健全方式,就是將執行緒程式和資料欄位放入背景工作物件。 如需詳細資訊,請參閱 在開始時間建立執行緒和傳遞資料

另請參閱

適用於