Advertisement



< Prev
Next >



C# Passing an Argument to a Thread






In the previous tutorial, we have explained how to create a simple thread, but we couldn't pass an argument to it. In this tutorial, we will discuss how to create a new thread and also pass an argument to it by calling a different constructor of Thread class, which is based on the ParameterizedThreadStart delegate, as shown below.

Constructor Description
public Thread(ParameterizedThreadStart 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.

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

    public delegate
    	void ParameterizedThreadStart(Object ob)

    Looking at the ParameterizedThreadStart delegate, your entry-point method be defined with a void return type i.e. it must not return anything but it can take an argument of Object type.







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 form doesn't take any argument, which was discussed in previous tutorial - Creating a Thread. But, to start a thread which takes an argument, we will have to call the version of Start() method which takes an argument, as shown below.

public void Start(Object parameter)

As you can see, this version of Start() method doesn't return anything, but it takes an argument of Object type. Let us show examples of how you could create a new thread and pass it an argument in a few different ways.


Advertisement




Creating a thread and passing it its name as an argument


In the upcoming example, we are going to create a new thread and will pass its name, as an argument to its entry-point method.

//C# Example of creating a thread and passing it an argument

using System;
using System.Threading;

class ThreadEx
{
Thread newThrd;

//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.
	newThrd = new Thread(this.Run);

	//Calling the Start() method, which calls the entry-point method defined with an argument
	//And passing it an argument i.e. name of the thread
	newThrd.Start(name);	
}

//Entry Point of the new thread.
//has a parameter of type object.
public void Run(Object name)  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;
		
	//Casting the  the Name property of Thread to set/get the name of our thread
	th.Name = (String)name;

	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");
}
}


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






Creating a thread and passing it a counter value


In the upcoming example, we are going to create a new thread and will pass it a counter value(which will be used as the counter value of a for loop in this thread), as an argument to its entry-point method.

//C# Example of creating a thread and passing it an argument

using System;
using System.Threading;

class ThreadEx
{
Thread newThrd;

//Constructor of ThreadEx class
public ThreadEx(String name, int counter)
{
	//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.
	newThrd = new Thread(this.Run);

	//Using the Name property of Thread class to set its name
	newThrd.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	//And passing it an argument i.e. counter of the for loop for this thread
	newThrd.Start(counter);	
}

//Entry Point of the new thread.
//has a parameter of type object.
public void Run(Object count)  
{
	//Getting the reference to the currently executing thread.
	Thread th = Thread.CurrentThread;
		
	Console.WriteLine(th.Name + " has stated its execution");
	
	//Casting the value of count(which was internally an int), from Object to int
	int c =(int)count;
	try
	{
		for(int i=0; i<c; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 1 second after printing a number
			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 our ThreadEx class
	//To use it to create our thread
	ThreadEx ob = new ThreadEx("My Thread", 4);
}
}


Output is -:


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





Please share this article -




< Prev
Next >
< C# Creating a Thread
Creating Multiple Threads>



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