Advertisement
Constructor | Description |
---|---|
public Thread(ParameterizedThreadStart start) | This constructor creates a new instance of Thread class, where:
|
public void Start(Object parameter)
Advertisement
//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");
}
}
My Thread has stated its execution
My Thread is going to sleep for 3 seconds
My Thread is awake and exits
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement