Advertisement
Constructor | Description |
---|---|
public Thread(ThreadStart start) | This constructor creates a new instance of Thread class, where:
|
public void Start()
Advertisement
//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();
}
}
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 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");
}
}
My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution
//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();
}
}
My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution
//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");
}
}
My Thread has stated its execution
My Thread printing 0
My Thread printing 1
My Thread printing 2
My Thread has finished its execution
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement