Threading - CSharp Multithreading
Threading - CSharp Multithreading
http://www.tuto rialspo int.co m/csharp/csharp_multithre ading .htm Co pyrig ht © tuto rials po int.co m
A thread is defined as the execution path of a prog ram. Each thread defines a unique flow of control. If your
application involves complicated and time consuming operations then it is often helpful to set different execution
paths or threads, with each thread performing a particular job.
T hreads are lig htweig ht proc esses. One common example of use of thread is implementation of concurrent
prog ramming by modern operating systems. Use of threads saves wastag e of CPU cycle and increase
efficiency of an application.
So far we have written prog rams where a sing le thread runs as a sing le process which is the running instance of
the application. However, this way the application can perform one job at a time. T o make it execute more than
one task at a time, it could be divided into smaller threads.
T he Unstarted State: it is the situation when the instance of the thread is created but the Start method
has not been called.
T he Ready State: it is the situation when the thread is ready to run and waiting CPU cycle.
T he Dead State: it is the situation when the thread has completed execution or has been aborted.
When a C# prog ram starts execution, the main thread is automatically created. T he threads created using the
T hread class are called the child threads of the main thread. You can access a thread using the
CurrentT hread property of the T hread class.
using System;
using System.Threading;
namespace MultithreadingApplication
{
class MainThreadProgram
{
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
th.Name = "MainThread";
Console.WriteLine("This is {0}", th.Name);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
This is MainThread
CurrentPrinciple Gets or sets the thread's current principal (for role-based security).
CurrentUICulture Gets or sets the current culture used by the Resource Manag er to look up
culture-specific resources at run-time.
ExecutionContext Gets an ExecutionContext object that contains information about the various
contexts of the current thread.
IsAlive Gets a value indicating the execution status of the current thread.
IsBackg round Gets or sets a value indicating whether or not a thread is a backg round thread.
IsT hreadPoolT hread Gets a value indicating whether or not a thread belong s to the manag ed thread
pool.
Manag edT hreadId Gets a unique identifier for the current manag ed thread.
T he following table shows some of the most commonly used methods of the T hread class:
Creating Threads
T hreads are created by extending the T hread class. T he extended T hread class then calls the Start() method
to beg in the child thread execution.
using System;
using System.Threading;
namespace MultithreadingApplication
{
class ThreadCreationProgram
{
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
}
When the above code is compiled and executed, it produces the following result:
T he following example demonstrates the use of the sleep() method for making a thread pause for a specific
period of time.
using System;
using System.Threading;
namespace MultithreadingApplication
{
class ThreadCreationProgram
{
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
// the thread is paused for 5000 milliseconds
int sleepfor = 5000;
Console.WriteLine("Child Thread Paused for {0} seconds",
sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
When the above code is compiled and executed, it produces the following result:
Destroying Threads
T he Abort() method is used for destroying threads.
T he runtime aborts the thread by throwing a T hreadAbortExc eption. T his exception cannot be caug ht, the
control is sent to the finally block, if any.
using System;
using System.Threading;
namespace MultithreadingApplication
{
class ThreadCreationProgram
{
public static void CallToChildThread()
{
try
{
}
catch (ThreadAbortException e)
{
Console.WriteLine("Thread Abort Exception");
}
finally
{
Console.WriteLine("Couldn't catch the Thread Exception");
}
When the above code is compiled and executed, it produces the following result: