Thread.Start 方法

定義

會讓執行緒被排程執行。

多載

名稱 Description
Start()

導致作業系統將目前實例的狀態變更為 Running

Start(Object)

會讓作業系統將目前實例的狀態變更為 Running,並可選擇性地提供一個物件,包含執行緒執行的方法所使用的資料。

Start()

導致作業系統將目前實例的狀態變更為 Running

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

例外狀況

討論串已經開始了。

目前可用的記憶體不足以啟動此執行緒。

範例

以下範例建立並啟動一個執行緒。

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 狀態,作業系統即可排程執行。 執行緒從提供給執行緒建構子的 or ParameterizedThreadStart 代理所代表ThreadStart的方法的第一行開始執行。 請注意,呼叫 不會 Start 阻擋呼叫執行緒。

Note

若此過載用於使用代理 ParameterizedThreadStart 建立的執行緒, null 則會傳遞給執行緒執行的方法。

一旦執行緒終止,就無法再用呼叫來 Start重新啟動。

另請參閱

適用於

Start(Object)

會讓作業系統將目前實例的狀態變更為 Running,並可選擇性地提供一個物件,包含執行緒執行的方法所使用的資料。

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

參數

parameter
Object

一個包含執行緒執行方法要使用的資料的物件。

例外狀況

討論串已經開始了。

目前可用的記憶體不足以啟動此執行緒。

這個討論串是用 ThreadStart 代理人而非 ParameterizedThreadStart 代理人創建的。

範例

以下範例 ParameterizedThreadStart 建立一個代理,包含靜態方法與實例方法。

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 狀態,作業系統即可排程執行。 執行緒從提供給執行緒建構子的 or ParameterizedThreadStart 代理所代表ThreadStart的方法的第一行開始執行。 請注意,呼叫 不會 Start 阻擋呼叫執行緒。

一旦執行緒終止,就無法再用呼叫來 Start重新啟動。

這種過載與 ParameterizedThreadStart 代理讓資料傳遞給執行緒程序變得容易,但此技術並非型別安全,因為任何物件都可以傳遞到此過載。 更穩健的方式是將執行緒程序與資料欄位同時放入工作者物件中。 更多資訊請參閱 「建立執行緒及開始時傳遞資料」。

另請參閱

適用於