C# Main Thread

Last Updated : 28 Apr, 2026

In C#, threads are the smallest units of execution that allow concurrent execution of code, enabling multiple tasks to run concurrently within a single process.

  • The Thread class in the System. Threading namespace is used to create and manage threads.
  • In C#, the Main method is the entry point of any console application. It is the first method that is executed when we run a C# program.

Example 1: Demonstration of a Single-Threaded Console Application with a Main Method.

C#
using System;
class Geeks
{
    // Main Thread
    static void Main()
    {
        Console.WriteLine("Hello, Geek");
    }
}

Output
Hello, Geek

Explanation: In the above example, the Main method simply writes the message "Hello, Geek" to the console using the Console.WriteLine method. The Main method must be a static method and can return either void or int. It may also accept a string array (string[] args) as a parameter for command-line arguments.

Example 2: Main Method That Takes Command-Line Arguments

C#
using System;
class Geeks
{
	static void Main(string[] args) {
		if (args.Length > 0) {
			Console.WriteLine("The first argument is: {0}"
                              , args[0]);
		} else {
			Console.WriteLine("No arguments were passed.");
		}
	}
}

Output:

No arguments were passed.

Main Thread

When a C# program starts up, one thread begins running immediately. This is usually called the main thread of our program.

Properties of the Main Thread:

  • It is the initial thread from which other threads can be created.
  • The main thread is not necessarily the last to finish; the application remains active as long as any foreground thread is still running.
MainThreadCsharp
Main Thread

Example: Main Thread and Child Threads

C#
using System;
using System.Threading;

public class Geeks
{
    // Main thread
    static public void Main()
    {
        Console.WriteLine("Welcome to the Main thread");

        // Child threads
        Thread thrA = new Thread(childThread);
        Thread thrB = new Thread(childThread);
        thrA.Start();
        thrB.Start();
    }

    public static void childThread()
    {
        Console.WriteLine("Welcome to the Child thread");
    }
}

Output
Welcome to the Main thread
Welcome to the Child thread
Welcome to the Child thread

Explanation:

  • The above program demonstrates how the main thread creates two child threads.
  • The main thread starts running immediately when the program begins, printing "Welcome to the Main thread."
  • Then, it creates two child threads (thrA and thrB) which print "Welcome to the Child thread."
  • These child threads run concurrently after being started by the Start() method.

Accessing the Main Thread

To access the main thread, you can use the Thread.CurrentThread property. This property returns a reference to the current thread. By using it inside the main thread, you can get a reference to the main thread itself. 

Example: Demonstration of How to access the main thread in C#

C#
using System;
using System.Threading;

public class Geeks
{
    // Main Method
    static public void Main()
    {
        Thread thr;

        // Get the reference of the main thread 
        // using the CurrentThread property
        thr = Thread.CurrentThread;

        // Display the name of the main thread
        if (thr.Name == null)
            Console.WriteLine("Main thread does not have a name");
        else
            Console.WriteLine("The name of the main thread is: {0}"
                              , thr.Name);

        Console.WriteLine();

        // Display the priority of the main thread
        Console.WriteLine("The priority of the main thread is: {0}"
                          , thr.Priority);

        // Set the name of the main thread
        thr.Name = "Main Thread";
        Console.WriteLine();

        // Display the name of the main thread after renaming it
        Console.WriteLine("The name of the main thread is: {0}"
                          , thr.Name);
    }
}

Output
Main thread does not have a name

The priority of the main thread is: Normal

The name of the main thread is: Main Thread

Explanation: This example demonstrates how to access the main thread using Thread.CurrentThread. It checks whether the main thread has a name (by default, it does not), displays the priority of the main thread, sets the name of the main thread, and then displays the new name.

Deadlocking Using the Main Thread

A deadlock occurs when two or more threads are waiting on each other indefinitely, preventing further execution, causing the program to hang. This can be demonstrated using Thread.CurrentThread.Join(), which tells the main thread to wait for itself to complete, resulting in a deadlock.

Example:

C#
using System;
using System.Threading;

public class Geeks
{
	public static void Main()
	{
		try
		{
			Console.WriteLine("Enter into DEADLOCK!!");

			Thread.CurrentThread.Join();

			// the following statement
			// will never execute
			Console.WriteLine("This statement will never execute");
		}

		catch (ThreadInterruptedException e)
		{
			e.ToString();
		}
	}
}

Output:

DeadLockCSharp
Output

Runtime Error:

The program hangs due to a deadlock caused by the thread waiting on itself.

Explanation: The statement “Thread.CurrentThread.Join();” , will tell the Main thread to wait for this thread (i.e. wait for itself) to die. Thus the Main thread waits for itself to die, which is nothing but a deadlock.

Comment

Explore