Advertisement



< Prev
Next >



C# Creating a Thread






The Thread class provides us multiple constructors, and depending on what constructor of you are calling, you could create a thread in a different way. Let us explain the simplest constructor of Thread class, which is based on ThreadStart delegate.

Constructor Description
public Thread(ThreadStart start)
This constructor creates a new instance of Thread class, where:
  • start specifies the name of a method, which will be called to begin the execution of a thread. This method can also be referred as the entry-point method of a thread.

  • ThreadStart specifies a delegate defined in .NET framework, as shown below.

    public delegate void ThreadStart()

    Looking at the ThreadStart delegate, your entry-point method must be defined with a void return type i.e. it must not return anything and it should not take any arguments either.







How to start a newly created Thread


Just creating a thread, does not start it. To start a newly created thread, we must call its Start() method, which will eventually call the entry-point method of a thread to execute it. This newly created thread finishes its execution when its entry-point method finishes its execution.

The Start() method has multiple forms and one of its forms is shown below.

public void Start()

As you can see, this version of Start() method doesn't return anything and it takes no arguments either. Let us show examples of how you could create a new thread in a few different ways.


Advertisement




Example of creating a thread


By calling the already explained constructor of the Thread class, let us show you an example of creating a thread and execute it. This newly created thread finishes its execution when its entry-point method finishes its execution.

//C# Example of creating a thread

using System;
using System.Threading;

class ThreadEx
{
String threadName;

//Constructor of our class
public ThreadEx(String name)
{
	threadName = name;
}


//Entry Point of the new thread.
public void Run()  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;
		
	//Using the Name property of Thread to set/get the name of our thread
	th.Name = threadName;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		Console.WriteLine(th.Name + " is going to sleep for 3 seconds");
		
		//Calling the Sleep() method on newly created thread
		//To make it sleep and suspend its execution for 3 seconds
		Thread.Sleep(3000);
		
		Console.WriteLine(th.Name + " is awake and exits");		
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of our ThreadEx class
	//To use it to create our thread
	ThreadEx ob = new ThreadEx("My Thread");

	//Creating our thread, based on the object of ThreadEx class
	//We are specifying the entry-point method that will be called to begin the execution of our thread.
	Thread th2 = new Thread(ob.Run);


	//Calling the Start() method, which automatically calls the entry-point method of the invoked thread
	th2.Start();
}
}


Output is -:


My Thread has stated its execution
My Thread is going to sleep for 3 seconds
My Thread is awake and exits


Program Analysis






Another way of creating a thread


In the previous example, we have created an object of a class i.e. ThreadEx, which was used to create a new thread. And, the name of new thread was first stored in the instance variable i.e. threadName of ThreadEx class and then it was assigned to the new thread by calling the Name property of Thread class.

In the upcoming example, we will not store the name of our thread in an instance variable, instead, we will directly assign it to the new thread by calling the Name property of Thread class(right after the new thread is created).

//C# Example of creating a new thread
//With its entry point method having a different name than the typical Run()

using System;
using System.Threading;

class ThreadEx
{
//Instance variable 
Thread thread;

//Constructor of ThreadEx class
public ThreadEx(String name)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Using the Name property of Thread class to set the name of new thread
	thread.Name = name;

	//Calling the Start() method to start the execution of the new thread
	thread.Start();
}
	
//Entry Point of the new thread.
public void Run()  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " printing " + i);
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend its execution for 3 seconds
			Thread.Sleep(1000);	
		}
		Console.WriteLine(th.Name + " has finished its execution");
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of ThreadEx class
	//To use it to create our thread
	ThreadEx ob = new ThreadEx("My Thread");
}
}


Output is -:


My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution


Program Analysis






One more way of creating the same thread


In the upcoming example, we will not be creating a thread or naming it within the constructor of a class, instead, we will create it in the Main() method and will directly assign it the name by calling the Name property of Thread class(right after the new thread is created).

Therefore, no constructor definition of a class is required in this example.

//C# Example of creating a new thread
//With its entry point method having a different name than the typical Run()

using System;
using System.Threading;

class ThreadEx
{
	
//Entry Point of the new thread.
public void Run()  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " printing " + i);
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend its execution for 3 seconds
			Thread.Sleep(1000);	
		}
		Console.WriteLine(th.Name + " has finished its execution");
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of ThreadEx class
	//To use it to create our thread
	ThreadEx ob = new ThreadEx();

	//Creating our thread, based on the object of ThreadEx class
	//We are specifying the entry-point method that will be called to begin the execution of our thread.
	Thread th2 = new Thread(ob.Run);

	//Setting the name of our newly created thread
	th2.Name = "My Thread";

	//Calling the Start() method, which automatically calls the entry-point method of the invoked thread
	th2.Start();
}
}


Output is -:


My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution





The entry-point method could have any name.


Irrespective of how you have created your thread, the entry-point method of a thread could be given any valid name and not necessarily be the Run() method(which is the case with some other languages). Let us show you an example.

//C# Example of creating a new thread
//With its entry point method having a different name than the typical Run()

using System;
using System.Threading;

class ThreadEx
{
//Instance variable 
Thread thread;

//Constructor of ThreadEx class
public ThreadEx(String name)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method i.e Go 
	//which will be called to begin the execution of our thread.
	thread = new Thread(this.Go);

	//Using the Name property of Thread class to set the name of new thread
	thread.Name = name;

	//Calling the Start() method to start the execution of the new thread
	thread.Start();
}
	
//Entry Point of the new thread.
public void Go()  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;

	Console.WriteLine(th.Name + " has stated its execution");

	try
	{
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " printing " + i);
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend its execution for 3 seconds
			Thread.Sleep(1000);	
		}
		Console.WriteLine(th.Name + " has finished its execution");
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of ThreadEx class
	//To use it to create our thread
	ThreadEx ob = new ThreadEx("My Thread");
}
}


Output is -:


My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution

As you can see in our last example, we have given the entry-point method a name, Go, instead of the typical name, Run. In the next tutorial, we will show you how to pass an argument to a thread by using a different form of constructor of Thread.



Please share this article -




< Prev
Next >
< Lifecycle of a Thread
Passing an argument to Thread >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement