Thread.Start Method
Causes the operating system to change the state of the current instance to ThreadState.Running.
[Visual Basic] Public Sub Start() [C#] public void Start(); [C++] public: void Start(); [JScript] public function Start();
Exceptions
| Exception Type | Condition |
|---|---|
| ThreadStateException | The thread has already been started. |
| SecurityException | The caller does not have the appropriate SecurityPermission. |
| OutOfMemoryException | There is not enough memory available to start this thread. |
| NullReferenceException | This method was invoked on a thread reference that is a null reference (Nothing in Visual Basic). |
Remarks
Once a thread is in the Running state, the operating system can schedule it for execution. When the thread begins executing, the ThreadStart delegate supplied to the constructor for the thread invokes its methods.
Once the thread terminates, it cannot be restarted with another call to Start.
Example
[Visual Basic, C#, C++] The following code example demonstrates creating a thread and starting it.
[Visual Basic] Imports System 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 'DoWork End Class 'ThreadWork Class ThreadTest Public Shared Sub Main() Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork) Dim myThread As New Thread(myThreadDelegate) myThread.Start() Dim i As Integer For i = 0 To 2 Console.WriteLine("In main.") Thread.Sleep(100) Next i End Sub 'Main End Class 'ThreadTest [C#] 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() { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); for(int i = 0; i<3; i++) { Console.WriteLine("In main."); Thread.Sleep(100); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Threading; public __gc class ThreadWork { public: static void DoWork() { for(int i = 0; i<3;i++) { Console::WriteLine(S"Working thread..."); Thread::Sleep(100); } } }; int main() { ThreadStart* myThreadDelegate = new ThreadStart(0, &ThreadWork::DoWork); Thread* myThread = new Thread(myThreadDelegate); myThread->Start(); for(int i = 0; i<3; i++) { Console::WriteLine(S"In main."); Thread::Sleep(100); } }
[Visual Basic, C#, C++] This code produces the following output:
In main.
Working thread...
In main. Working thread...
In main.
Working thread... [Visual Basic, C#, C++] Note that the sequence of the output statements is typical, but is not guaranteed to be identical across systems.
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Thread Class | Thread Members | System.Threading Namespace | Creating Threads