Thread.Start 方法

定义

使线程得以按计划执行。Causes a thread to be scheduled for execution.

重载

Start()

导致操作系统将当前实例的状态更改为 RunningCauses the operating system to change the state of the current instance to Running.

Start(Object)

导致操作系统将当前实例的状态更改为 Running,并选择提供包含线程执行的方法要使用的数据的对象。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()

导致操作系统将当前实例的状态更改为 RunningCauses 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 ()

异常

线程已启动。The thread has already been started.

内存不足,无法启动此线程。There is not enough memory available to start this 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...

注解

线程处于 ThreadState.Running 状态后,操作系统就可以将其计划为执行。Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution. 线程开始在由提供给线程构造函数的 ThreadStartParameterizedThreadStart 委托表示的方法的第一行执行。The thread begins executing at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor. 请注意,对 Start 的调用不会阻止调用线程。Note that the call to Start does not block the calling thread.

备注

如果此重载与使用 ParameterizedThreadStart 委托创建的线程一起使用,null 将传递给由该线程执行的方法。If this overload is used with a thread created using a ParameterizedThreadStart delegate, null is passed to the method executed by the thread.

线程终止后,不能再次调用 StartOnce the thread terminates, it cannot be restarted with another call to Start.

另请参阅

Start(Object)

导致操作系统将当前实例的状态更改为 Running,并选择提供包含线程执行的方法要使用的数据的对象。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)

参数

parameter
Object

一个对象,包含线程执行的方法要使用的数据。An object that contains data to be used by the method the thread executes.

异常

线程已启动。The thread has already been started.

内存不足,无法启动此线程。There is not enough memory available to start this thread.

该线程是使用 ThreadStart 委托而不是 ParameterizedThreadStart 委托创建的。This thread was created using a ThreadStart delegate instead of a ParameterizedThreadStart delegate.

示例

下面的示例使用静态方法和实例方法创建 ParameterizedThreadStart 委托。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.'

注解

线程处于 ThreadState.Running 状态后,操作系统就可以将其计划为执行。Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution. 线程开始在由提供给线程构造函数的 ThreadStartParameterizedThreadStart 委托表示的方法的第一行执行。The thread begins executing at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor. 请注意,对 Start 的调用不会阻止调用线程。Note that the call to Start does not block the calling thread.

线程终止后,不能再次调用 StartOnce the thread terminates, it cannot be restarted with another call to Start.

利用此重载和 ParameterizedThreadStart 委托,可以轻松地将数据传递给线程过程,但该方法不是类型安全的,因为任何对象都可以传递到此重载。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. 将数据传递给线程过程的一种更可靠的方法是将线程过程和数据字段放入辅助角色对象。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. 有关详细信息,请参阅在启动时创建线程和传递数据For more information, see Creating Threads and Passing Data at Start Time.

另请参阅

适用于