Compartilhar via


Thread.Start Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Sub Start
public void Start()

Exceptions

Exception Condition
ThreadStateException

The thread has already been started.

OutOfMemoryException

There is not enough memory available to start this thread.

Remarks

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.

NoteNote:

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

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

Examples

This section contains two code examples. The first example shows how to create and start a thread that executes a static method. The second example shows how to create and start a thread that executes an instance method.

The examples display their output in a TextBlock on the user interface (UI) thread. To access the TextBlock from the callback thread, the examples use the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then use 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.

Example 1

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

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 ThreadStart
      ' delegate. Visual Basic expands the AddressOf expression 
      ' to the appropriate delegate creation syntax:
      '    New ThreadStart(AddressOf Example.DoWork)
      '
      Dim newThread As New Thread(AddressOf Example.DoWork)
      newThread.Start()

   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()
      Dim display As New Action(Of String)(AddressOf DisplayOutput)

      outputBlock.Dispatcher.BeginInvoke(display, _
         "Hello from a Shared thread procedure." & vbCrLf)
   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:
'
'Hello from a Shared thread procedure.
using System;
using System.Threading;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      // To start a thread using a static thread procedure, use the
      // class name and method name when you create the ThreadStart
      // delegate. C# expands the method name to the appropriate 
      // delegate creation syntax:
      //    New ThreadStart(Example.DoWork)
      //
      Thread newThread = new Thread(Example.DoWork);
      newThread.Start();
   }

   // 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 static void DoWork()
   {
      outputBlock.Dispatcher.BeginInvoke(delegate () { 
         outputBlock.Text += "Hello from a static thread procedure.\n"; 
      });
   }
}

/* This code example produces the following output:

Hello from a static thread procedure.
 */

Example 2

The following example shows how to create and start a thread that executes an instance method.

Imports System.Threading

Public Class Example

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

      ' To start a thread using an instance method for the thread 
      ' procedure, use the instance variable and method name when 
      ' you create the ThreadStart delegate. Visual Basic expands 
      ' the AddressOf expression to the appropriate delegate 
      ' creation syntax:
      '    New ThreadStart(AddressOf w.DoMoreWork)
      '
      Dim w As New Work()
      w.Data = 42
      w.Output = outputBlock

      Dim newThread As Thread = New Thread(AddressOf w.DoMoreWork)
      newThread.Start()
   End Sub

End Class

Public Class Work
   Public Data As Integer
   Public Output As System.Windows.Controls.TextBlock

   ' 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.
   Public Sub DoMoreWork()
      Dim display As New Action(Of String)(AddressOf DisplayOutput)

      Output.Dispatcher.BeginInvoke(display, _
         String.Format("Instance thread procedure. Data={0}", Data) & vbCrLf)
   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 Sub DisplayOutput(msg)
      Output.Text &= msg
   End Sub

End Class

' This code example produces the following output:
'
'Instance thread procedure. Data=42
using System;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // To start a thread using an instance method for the thread 
      // procedure, use the instance variable and method name when 
      // you create the ThreadStart delegate. C# expands the object
      // reference and method name to the appropriate delegate 
      // creation syntax:
      //    New ThreadStart(AddressOf w.DoMoreWork)
      //
      Work w = new Work();
      w.Data = 42;
      w.Output = outputBlock;

      Thread newThread = new Thread(w.DoMoreWork);
      newThread.Start();
   }
}

public class Work
{
   public int Data;
   public System.Windows.Controls.TextBlock Output;

   // 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.
   public void DoMoreWork()
   {
      Output.Dispatcher.BeginInvoke(delegate () {
         Output.Text += String.Format("Instance thread procedure. Data={0}\n", Data);
      });
   }
}

// This code example produces the following output:
//
//Instance thread procedure. Data=42

Version Information

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

Platforms

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