Advertisement
//C# Example of Interrupt() method
using System;
using System.Threading;
class ThreadEx
{
Thread thread;
//Constructor of our 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);
//Setting the name of a thread by using the Name property of Thread
thread.Name = name;
//Calling the Start() method, which calls the entry-point method defined with an argument
thread.Start();
}
public void Run()
{
//Getting the reference of the currently executing thread
Thread th = Thread.CurrentThread;
try
{
Console.WriteLine(th.Name + " has started its execution");
for(int i=0; i<3; i++)
{
Console.WriteLine(th.Name + " is printing " + i);
//Calling the Sleep() method on newly created thread
//To make it sleep and suspend for 2 seconds after printing a number
Thread.Sleep(2000);
}
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
//where each object is used to construct a separate thread
ThreadEx ob1 = new ThreadEx("My Thread1");
//Main thread calling Interrupt() on the new Thread
//This will throw ThreadInterruptedException when the new Thread is goes to a blocked state i.e. WaitSleepJoin
ob1.thread.Interrupt();
//Main thread has called Join() method on the new thread
//To wait until its execution is complete
ob1.thread.Join();
}
}
My Thread1 has started its execution
My Thread1 is printing 0
Thread InterruptedSystem.Threading.ThreadInterruptedException: Thread was interrupted from a waiting state.
at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
at ThreadEx.Run()
Advertisement
//C# Example of calling the Interrupt() method on a child thread
//Which goes to a blocked state by calling Join() on the Main Thread
//Which makes this child thread wait for the Main Thread to finish its execution
using System;
using System.Threading;
class ThreadEx
{
Thread thread;
//Constructor of our class
public ThreadEx(String name, Thread main)
{
//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);
//Setting the name of a thread by using the Name property of Thread
thread.Name = name;
//Calling the Start() method, which calls the entry-point method defined with an argument
//And passing it an argument i.e. reference to the Main Thread
thread.Start(main);
}
//Entry Point of the new thread.
public void Run(Object main)
{
try
{
//Getting the reference to the currently executing thread.
Thread th = Thread.CurrentThread;
Console.WriteLine(th.Name + " has started its execution");
//Casting the value of main(which was internally an Thread), from Object to Thread
Thread th2 = (Thread)main;
Console.WriteLine(th2.Name + " is being interrupted");
//This child thread is calling the Join() method on the Main thread
//Which puts it in a blocked state i.e. waiting for the Main thread to finish its execution
th2.Join();
Console.WriteLine(th.Name + " has finished its execution ");
}
catch(Exception e)
{
Console.WriteLine("Exception caught: " + e);
}
}
public static void Main(String[] ar)
{
//Getting the reference of the automatically created currently executing Main thread
Thread mainThread = Thread.CurrentThread;
//Setting the name of the Main threadss
mainThread.Name = "Main Thread";
//Creating an objects of our ThreadEx class
//where each object is used to construct a separate thread
//And passing the reference of the Main thread
ThreadEx ob1 = new ThreadEx("My Thread1", mainThread);
//Main thread calling Interrupt() on the new Thread
//This will throw ThreadInterruptedException when the new Thread is goes to a blocked state i.e. WaitSleepJoin
ob1.thread.Interrupt();
}
}
My Thread1 has started its execution
Main Thread is being interrupted
Exception caught: System.Threading.ThreadInterruptedException: Thread was interrupted from a waiting state.
at System.Threading.Thread.JoinInternal(Int32 millisecondsTimeout)
at ThreadEx.Run(Object main)
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement