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. Execution begins at the first line of the method that is represented by the ThreadStart or ParameterizedThreadStart delegate.

Note:

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.

This section contains two examples. The first example shows how to use a ThreadStart delegate to execute a static method, and the second shows how to use a ThreadStart delegate to execute an instance method.

using System;
using System.Threading;
publicclass Example
{
privatestatic System.Windows.Controls.TextBlock outputBlock;
publicstaticvoid 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.privatestaticvoid 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 code example shows how to use a ThreadStart delegate to execute an instance method.

using System;
using System.Threading;
publicclass Example
{
publicstaticvoid 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();
}
}
publicclass Work
{
publicint 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.publicvoid 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