Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Thread.Start Method ()

Causes the operating system to change the state of the current instance to ThreadState.Running.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public void Start ()
public void Start ()
public function Start ()
Exception typeCondition

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.

NoteNote

The HostProtectionAttribute attribute applied to this method has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution. The thread begins executing at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor.

NoteNote

If this overload is used with a thread created using a ParameterizedThreadStart delegate, a null reference (Nothing in Visual Basic) is passed to the method executed by the thread.

Once the thread terminates, it cannot be restarted with another call to Start.

The following code example demonstrates creating a thread and starting it.

This code produces the following output:

     In main.
     Working thread...
     In main. Working thread...
     In main.
     Working thread...

Note that the sequence of the output statements is typical, but is not guaranteed to be identical across systems.

Thread procedures can be static methods or instance methods. See the code example provided for the ThreadStart delegate. For more information about thread creation, see Creating Threads and Passing Data at Start Time.

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);
         }
   }
}

import System.*;
import System.Threading.*;
public class ThreadWork
{
    public static void DoWork()
    {
        for (int i = 0; i < 3; i++) {
            Console.WriteLine("Working thread...");
            System.Threading.Thread.Sleep(100);
        }
    } //DoWork
} //ThreadWork

class ThreadTest
{
    public static void main(String[] args)
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        System.Threading.Thread myThread =
            new System.Threading.Thread(myThreadDelegate);
        myThread.Start();
        for (int i = 0; i < 3; i++) {
            Console.WriteLine("In main.");
            System.Threading.Thread.Sleep(100);
        }
    } //main
} //ThreadTest

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.