Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Multithreading

C# Multithreading

Thread is defined as a path of execution of a program. Each thread defines a unique control flow. If your application involves complex and time-consuming operations, it is often beneficial to set up different thread execution paths, with each thread performing a specific job.

A thread is alightweight process. A common example of using threads is the implementation of parallel programming in modern operating systems. Using threads saves wasted CPU cycles while improving application efficiency.

The programs we have written so far run as a single process with a single thread as the running instance of the application. However, such an application can only execute one task at a time. To execute multiple tasks simultaneously, it can be divided into smaller threads.

Thread Lifecycle

The thread lifecycle begins when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.

The various states in the thread lifecycle are listed below:

- the Sleep method has been called - the Wait method has been called - blocked by an I/O operation

Main thread

In C#,System.Threading.Thread The class is used for thread work. It allows creating and accessing individual threads in a multithreaded application. The first thread executed in a process is called theMain thread。

When a C# program starts executing, the main thread is automatically created. Threads created using the Thread class are called as child threads of the main thread. You can use the Thread class's CurrentThread property to access a thread.

The following program demonstrates the execution of the main thread:

Example

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 code above is compiled and executed, it produces the following result:



This is MainThread

Common Properties and Methods of the Thread Class

The following table lists the Thread Some commonly used members of the class property:

property Description
CurrentContext Gets the current context in which the thread is executing.
CurrentCulture Gets or sets the culture of the current thread.
CurrentPrincipal Gets or sets the current principal of the thread (for role-based security).
CurrentThread Gets the currently running thread.
CurrentUICulture Gets or sets the current culture used by the resource manager to look up culture-specific resources at runtime.
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.
IsBackground Gets or sets a value indicating whether a thread is a background thread.
IsThreadPoolThread Gets a value indicating whether a thread belongs to the managed thread pool.
ManagedThreadId Gets the unique identifier of the current managed thread.
Name Gets or sets the name of the thread.
Priority Gets or sets a value indicating the scheduling priority of a thread.
ThreadState Gets a value containing the status of the current thread.

The following table lists the Thread Some commonly used members of the class Method:

No. Method Name & Description
1 public void Abort()
Raises a ThreadAbortException on the thread calling this method, to begin the process of terminating the thread. Calling this method usually terminates the thread.
2 public static LocalDataStoreSlot AllocateDataSlot()
Allocates an unnamed data slot on all threads. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
3 **public static LocalDataStoreSlot AllocateNamedDataSlot( string name) **
Allocates a named data slot on all threads. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
4 public static void BeginCriticalRegion()
Notifies the host that execution is about to enter a code region in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
5 public static void BeginThreadAffinity()
Notifies the host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread.
6 public static void EndCriticalRegion()
Notifies the host that execution is about to enter a code region in which the effects of a thread abort or unhandled exception are limited to the current task.
7 public static void EndThreadAffinity()
Notifies the host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread.
8 public static void FreeNamedDataSlot(string name)
Eliminates the association between a name and a slot for all threads in the process. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
9 **public static Object GetData( LocalDataStoreSlot slot ) **
Retrieves the value from the specified slot on the current thread, within the current domain of the current thread. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
10 public static AppDomain GetDomain()
Returns the current domain in which the current thread is running.
11 public static AppDomain GetDomainID()
Returns the unique application domain identifier.
12 **public static LocalDataStoreSlot GetNamedDataSlot( string name ) **
Searches for a named data slot. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
13 public void Interrupt()
Interrupts a thread that is in the WaitSleepJoin thread state.
14 public void Join()
Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage message pumping. This method has different overloads.
15 public static void MemoryBarrier()
Synchronizes memory access as follows: the processor executing the current thread cannot reorder instructions in such a way that memory accesses after the MemoryBarrier call execute before memory accesses before the MemoryBarrier call.
16 public static void ResetAbort()
Cancels the Abort requested for the current thread.
17 **public static void SetData( LocalDataStoreSlot slot, Object data ) **
Sets data in the specified slot on the currently running thread, for the current domain of this thread. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
18 public void Start()
Starts a thread.
19 **public static void Sleep( int millisecondsTimeout ) **
Causes a thread to pause for a period of time.
20 **public static void SpinWait( int iterations ) **
Causes the thread to wait for the amount of time defined by the iterations parameter.
21 **public static byte VolatileRead( ref byte address )

public static double VolatileRead( ref double address )
public static int VolatileRead( ref int address )
public static Object VolatileRead( ref Object address ) **
Reads a field value. Regardless of the number of processors or the state of processor caches, the value is the most recent one written by any processor on the computer. This method has different overloads. Only some forms are given here. | | 22 | **public static void VolatileWrite( ref byte address, byte value )
public static void VolatileWrite( ref double address, double value )
public static void VolatileWrite( ref int address, int value )
public static void VolatileWrite( ref Object address, Object value ) **
Immediately writes a value to a field so that the value is visible to all processors on the computer. This method has different overloads. Only some forms are given here. | | 23 | public static bool Yield()
Causes the calling thread to execute another thread that is ready to run on the current processor. The operating system chooses the thread to execute. |

Create a thread

A thread is created by extending the Thread class. The extended Thread class calls the Start() method to begin execution of the child thread.

The following program demonstrates this concept:

Example

using

System

;

using

System.Threading

;

namespace

MultithreadingApplication

{

   

class

ThreadCreationProgram

   

{

       

public

static

void

CallToChildThread

(

)

       

{

            Console

.

WriteLine

(

"Child thread starts"

)

;

       

}

       

       

static

void

Main

(

string

[

]

args

)

       

{

            ThreadStart childref

=

new

ThreadStart

(

CallToChildThread

)

;

            Console

.

WriteLine

(

"In Main: Creating the Child thread"

)

;

            Thread childThread

=

new

Thread

(

childref

)

;

            childThread

.

Start

(

)

;

            Console

.

ReadKey

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



In Main: Creating the Child thread

Child thread starts

Managing Threads

The Thread class provides various methods for managing threads.

The following example demonstrates sleep() method is used to pause a thread for a specific time.

Example

using

System

;

using

System.Threading

;

namespace

MultithreadingApplication

{

   

class

ThreadCreationProgram

   

{

       

public

static

void

CallToChildThread

(

)

       

{

            Console

.

WriteLine

(

"Child thread starts"

)

;

           

// Pause the thread for 5000 milliseconds

           

int

sleepfor

=

5000

;

            Console

.

WriteLine

(

"Child Thread Paused for {0} seconds"

,

                              sleepfor

/

1000

)

;

            Thread

.

Sleep

(

sleepfor

)

;

            Console

.

WriteLine

(

"Child thread resumes"

)

;

       

}

       

       

static

void

Main

(

string

[

]

args

)

       

{

            ThreadStart childref

=

new

ThreadStart

(

CallToChildThread

)

;

            Console

.

WriteLine

(

"In Main: Creating the Child thread"

)

;

            Thread childThread

=

new

Thread

(

childref

)

;

            childThread

.

Start

(

)

;

            Console

.

ReadKey

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



In Main: Creating the Child thread

Child thread starts

Child Thread Paused for 5 seconds

Child thread resumes

Destroy a thread

Abort() method is used to destroy a thread.

by throwing a threadabortexception to abort a thread at runtime. This exception cannot be caught; if there is a finally block, control is transferred to the finally block.

The following program illustrates this:

Example

using

System

;

using

System.Threading

;

namespace

MultithreadingApplication

{

   

class

ThreadCreationProgram

   

{

       

public

static

void

CallToChildThread

(

)

       

{

           

try

           

{

                Console

.

WriteLine

(

"Child thread starts"

)

;

               

// Count to 10

               

for

(

int

counter

=

0

;

counter

<=

10

;

counter

++

)

               

{

                    Thread

.

Sleep

(

500

)

;

                    Console

.

WriteLine

(

counter

)

;

               

}

                Console

.

WriteLine

(

"Child Thread Completed"

)

;

           

}

           

catch

(

ThreadAbortException e

)

           

{

                Console

.

WriteLine

(

"Thread Abort Exception"

)

;

           

}

           

finally

           

{

                Console

.

WriteLine

(

"Couldn't catch the Thread Exception"

)

;

           

}

       

}

       

       

static

void

Main

(

string

[

]

args

)

       

{

            ThreadStart childref

=

new

ThreadStart

(

CallToChildThread

)

;

            Console

.

WriteLine

(

"In Main: Creating the Child thread"

)

;

            Thread childThread

=

new

Thread

(

childref

)

;

            childThread

.

Start

(

)

;

           

// Stop the main thread for a while

            Thread

.

Sleep

(

2000

)

;

           

// Now abort the child thread

            Console

.

WriteLine

(

"In Main: Aborting the Child thread"

)

;

            childThread

.

Abort

(

)

;

            Console

.

ReadKey

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



In Main: Creating the Child thread

Child thread starts

0

1

2

In Main: Aborting the Child thread

Thread Abort Exception

Couldn't catch the Thread Exception