Thread.Start Method (Object)

Causes the operating system to change the state of the current instance to ThreadState.Running, and optionally supplies an object containing data to be used by the method that the thread executes.

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

'Declaration
Public Sub Start ( _
	parameter As Object _
)

Parameters

parameter
Type: System.Object
An object that contains data to be used by the method that the thread executes.

ExceptionCondition
ThreadStateException

The thread has already been started.

OutOfMemoryException

There is not enough memory available to start this thread.

InvalidOperationException

This thread was created by using a ThreadStart delegate instead of a ParameterizedThreadStart delegate.

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 that is represented by the ThreadStart or ParameterizedThreadStart delegate supplied to the thread constructor.

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

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.

The following example shows how to create and start a thread that executes a static method and passes data to the method.

The example displays its output in a TextBlock on the user interface (UI) thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher.BeginInvoke method to make the cross-thread call.

For more information about thread creation, see Creating Threads and Passing Data at Start Time. For additional examples of how to pass thread synchronization objects to thread procedures, see EventWaitHandle.


Imports System.Threading

Public Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      ' To start a thread using a static thread procedure, use the
      ' class name and method name when you create the 
      ' ParameterizedThreadStart delegate. Visual Basic expands the 
      ' AddressOf expression to the appropriate delegate creation 
      ' syntax:
      '    New ParameterizedThreadStart(AddressOf Example.DoWork)
      '
      Dim newThread As New Thread(AddressOf Example.DoWork)
      newThread.Start(42)

   End Sub

   ' Simulate work. To communicate with objects on the UI thread, get the 
   ' Dispatcher for one of the UI objects. Use the Dispatcher object's 
   ' BeginInvoke method to queue a delegate that will run on the UI thread,
   ' and therefore can safely access UI elements like the TextBlock.
   Private Shared Sub DoWork(ByVal state As Object)

      Dim data As Integer = CInt(state)
      Dim display As New Action(Of String)(AddressOf DisplayOutput)

      outputBlock.Dispatcher.BeginInvoke(display, _
         String.Format("Shared thread procedure. Data={0}" & vbLf, data))

   End Sub

   ' The Dispatcher.BeginInvoke method runs this helper method on the 
   ' UI thread, so it can safely access the TextBlock that is used to 
   ' display the output.
   Private Shared Sub DisplayOutput(msg)
      outputBlock.Text &= msg
   End Sub

End Class

' This code example produces the following output:
'
'Shared thread procedure. Data='42'


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: