C# ThreadStart

Threads

ThreadStart specifies a target method. It does not support any arguments to the method. We use the ThreadStart type. We create an instance of it and pass that to the Thread constructor.

Example

First, this example C# program creates an array of four different threads. It starts a parameterless method on each thread. It then joins the threads together in a sequential order.

Array

Next, we see that ThreadStart is created with a constructor that receives a function name as its only argument: the target method. This target method must not receive any formal parameters.

Then:The ThreadStart instance is passed as an argument to the Thread constructor.

Constructor
C# program that uses ThreadStart

using System;
using System.Threading;

class Program
{
    static void Main()
    {
	// Create an array of Thread references.
	Thread[] array = new Thread[4];
	for (int i = 0; i < array.Length; i++)
	{
	    // Start the thread with a ThreadStart.
	    ThreadStart start = new ThreadStart(Start);
	    array[i] = new Thread(start);
	    array[i].Start();
	}
	// Join all the threads.
	for (int i = 0; i < array.Length; i++)
	{
	    array[i].Join();
	}
	Console.WriteLine("DONE");
    }

    static void Start()
    {
	// This method has no formal parameters.
	Console.WriteLine("Start()");
    }
}

Output

Start()
Start()
Start()
Start()
DONE

Discussion

Join objects together

Using this same general example, we explain the Join method in more detail, also on this site. Please visit the appropriate article for a more thorough explanation of the Join method on the Thread type in the C# language.

Thread Join Method

Summary

The C# programming language

The ThreadStart type enables you to start a thread and pass no arguments to the target method. For parameterless target methods, this type is ideal, and you should pass it to the Thread constructor.

Tip:For more detailed information on threading in the C# language and .NET Framework, check out the thread category on this site.

ParameterizedThreadStart

C#: .NET: Thread