Open In App

How to Schedule a Thread for Execution in C#?

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, we can schedule a thread for execution using the Thread class. The Thread.Start() method is responsible for scheduling a thread, allowing it to run when system resources are available. This method can be overloaded to pass parameters to the thread. Also, the Thread.Sleep() method can be used to pause execution for a specified duration.

1. Using Thread.Start()

The Start() method changes the state of the current instance to Running, allowing the thread to execute its assigned task.

Syntax:

public void Start();

Exceptions:

  • ThreadStateException: If the thread has already been started.
  • OutOfMemoryException: If there is not enough memory available to start a thread.

Example:

C#
// C# program to illustrate the 
// use of Start() method 
using System; 
using System.Threading; 

public class Geeks 
{ 
	public static void Main() 
	{ 
		// Creating and initializing a thread 
		Thread thr = new Thread(new ThreadStart(Job)); 

		// Start the execution of Thread 
		// Using Start() method 
		thr.Start(); 
	} 
    
    public static void Job() 
	{ 
		for (int i = 0; i < 4; i++) { 
			Console.WriteLine(i); 
		} 
	} 
} 

Output
0
1
2
3

2. Using Thread.Start(Object)

This method starts the execution of a thread and allows passing an object containing data to be used by the method the thread executes.

Syntax:

public void Start(object parameter);

Exceptions:

  • ThreadStateException: If the thread has already been started.
  • OutOfMemoryException: If there is not enough memory available to start a thread.
  • InvalidOperationException: If the thread was created using a ThreadStart delegate instead of a ParameterizedThreadStart delegate.

Example:

C#
// C# program to illustrate the 
// use of Start(Object) method 
using System;
using System.Threading;

class Geeks
{
	public static void Main()
	{
		// Creating object of Geeks class 
		Geeks obj = new Geeks();

		// Creating and initializing threads 
		Thread thr1 = new Thread(obj.Job1);
		Thread thr2 = new Thread(Job2);

		// Start the execution of Thread 
		// Using Start(Object) method 
		thr1.Start(01);
		thr2.Start("Hello");
	}
	public void Job1(object value)
	{
		Console.WriteLine("Data of Thread 1 is: {0}", 
		value);
	}

	public static void Job2(object value)
	{
		Console.WriteLine("Data of Thread 2 is: {0}", 
		value);
	}
}

Output
Data of Thread 1 is: 1
Data of Thread 2 is: Hello

3. Using Thread.Sleep()

The Thread.Sleep() method pauses the execution of the current thread for a specified duration, allowing other threads to execute.

Syntax:

public static void Sleep(int millisecondsTimeout);

Example:

C#
// C# Program to illustrate the
// Thread.Sleep method
using System;
using System.Threading;

class Geeks
{
    static void Main()
    {
        Console.WriteLine("Starting...");
        Thread.Sleep(300); 
        Console.WriteLine("300 milliseconds have passed.");
    }
}

Output
Starting...
300 milliseconds have passed.

Next Article

Similar Reads