Thread.Start 方法

定义

使线程计划执行。

重载

名称 说明
Start()

使操作系统将当前实例 Running的状态更改为 。

Start(Object)

使操作系统将当前实例 Running的状态更改为,并选择性地提供一个对象,其中包含线程执行的方法要使用的数据。

Start()

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

使操作系统将当前实例 Running的状态更改为 。

public:
 void Start();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Start();
public void Start();
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Start : unit -> unit
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 状态后,操作系统可以计划其执行。 线程开始在提供给线程构造函数的方法ThreadStartParameterizedThreadStart的第一行处执行。 请注意,调用 Start 不会阻止调用线程。

注释

如果此重载与使用 ParameterizedThreadStart 委托创建的线程一起使用, null 则传递给线程执行的方法。

线程终止后,无法使用另一个调用 Start来重新启动它。

另请参阅

适用于

Start(Object)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

使操作系统将当前实例 Running的状态更改为,并选择性地提供一个对象,其中包含线程执行的方法要使用的数据。

public:
 void Start(System::Object ^ parameter);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Start(object? parameter);
public void Start(object? parameter);
public void Start(object parameter);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Start : obj -> unit
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 状态后,操作系统可以计划其执行。 线程开始在提供给线程构造函数的方法ThreadStartParameterizedThreadStart的第一行处执行。 请注意,调用 Start 不会阻止调用线程。

线程终止后,无法使用另一个调用 Start来重新启动它。

通过此重载和 ParameterizedThreadStart 委托,可以轻松地将数据传递到线程过程,但技术类型不安全,因为任何对象都可以传递给此重载。 将数据传递到线程过程的更可靠方法是将线程过程和数据字段都放入工作器对象。 有关详细信息,请参阅 在开始时间创建线程和传递数据

另请参阅

适用于