ParameterizedThreadStart Delegate

July 28, 2014

Represents the method that executes on a Thread.

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

public delegate void ParameterizedThreadStart(
	Object obj
)

Parameters

obj
Type: System.Object
An object that contains data for the thread procedure.

When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate or a ParameterizedThreadStart delegate that is passed to the Thread constructor. The thread does not begin executing until the Thread.Start method is called. The ThreadStart or ParameterizedThreadStart delegate is invoked on the thread, and execution begins at the first line of the method that is represented by the delegate. In the case of the ParameterizedThreadStart delegate, the object that is passed to the Start(Object) method is passed to the delegate.

NoteNote:

Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#, just specify the name of the thread procedure. The compiler selects the correct delegate constructor.

The ParameterizedThreadStart delegate and the Thread.Start(Object) method overload make it easy to pass data to a thread procedure, but this technique is not type safe because any object can be passed to Thread.Start(Object). 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 use a ParameterizedThreadStart delegate to execute a static method on a new thread.

The example displays its output in a TextBlock on the 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.


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 shared thread procedure, use
      // the class name and method name when you create the 
      // ParameterizedThreadStart delegate. C# infers the 
      // appropriate delegate creation syntax:
      //    New ParameterizedThreadStart(Example.DoWork)
      //
      Thread newThread = new Thread(Example.DoWork);
      newThread.Start(42);
   }

   // 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(object state)
   {
      int data = (int) state;

      outputBlock.Dispatcher.BeginInvoke(delegate () { 
         outputBlock.Text += String.Format("Static thread procedure. Data={0}\n", data); 
      });
   }
}

/* This code example produces the following output:

Static thread procedure. Data=42
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft