Advertisement



< Prev
Next >



C# Threads




A thread is a program in execution that is performing some specific task. In a C#, a thread is automatically created on the execution of a program and it is referred to as the main thread. In order to execute a C# program, the main thread looks for the Main() method in it.

This thread is called main thread because the Main() method of a C# program is the first method that runs in it. From the main thread, we can create other threads to perform multithreading.

Note : The main thread is provided to us by default, even when we didn't manually create a thread in a program. Besides the default given main thread, we can create our own thread by creating an object of Thread class, which is defined within the System.Threading namespace.




Multithreaded programming


C# allows us to perform multithreaded programming. A multithreaded program contains multiple threads running concurrently within a program, where each thread is performing some specific task. For example - a thread is performing text editing, a second thread is playing music and a third thread is printing a file in the background.

Hence, a multi-threading in a C# program helps in achieving executing multiple tasks at one time, thereby, multithreaded programs make the most use of CPU efficiency and keeps its idle time to minimum and that's why multithreaded programming with multiple threads running at the same time are used in making interactive applications.




Some useful methods of Thread class.


.
Methods Description

void Start()

This method causes a thread to be scheduled for execution.

void Abort()

This method usually terminates the thread by raising a ThreadAbortException in the thread on which it is invoked.

void Sleep(int milliseconds)

This method suspends the execution of a thread for the specified amount of milliseconds.

void Join()

This method blocks the calling thread until the thread represented by this instance terminates.

void Interrupt()

This method interrupts a thread that is in the WaitSleepJoin thread state.





Some useful properties of Thread class.


Properties Description

Name

This property allows us to get/set the name of a thread.

IsAlive

This property gets a bool value indicating the execution status of the current thread.

Priority

This property gets or sets a value indicating the scheduling priority of a thread.

CurrentThread

This property gets the reference to the currently executing thread.

ThreadState

This property gets the present state of the current thread. The initial value is Unstarted.





Accessing the default main thread and getting, setting its name -:


We are going to get the default name of the main thread and then, we will its set name, by using the Name property of the Thread class.

//C# Accessing the default thread, to get and set its name 

using System;
using System.Threading;

class ThreadExm
{
public static void Main(String[] ar)
{
	//Getting the reference of the default created Main Thread
	//By using the CurrentThread property of Thread class
	Thread th = Thread.CurrentThread;

	//Getting the name of the default created Main Thread
	//By using the Name property of Thread class
	Console.WriteLine("Default name of the main thread - "+ th.Name);

	//Setting the name of the thread	
	//By using the Name property of Thread class
	th.Name = "Main Thread";

	//Getting the just given mame to the default created Main Thread
	//By using the Name property of Thread class
	Console.WriteLine("New name of main thread - "+ th.Name);
}
}


Output is -:


Default name of the main thread -
New name of main thread - Main Thread


Program Analysis





Advertisement




Making the currently executing thread, sleep.


We will call the static Sleep() method of Thread class to make the currently executing, sleep.

//C# Accessing the default thread, to get and set its name 

using System;
using System.Threading;

class ThreadExm
{
public static void Main(String[] ar)
{
	//Getting the reference of the default created Main Thread
	//By using the CurrentThread property of Thread class
	Thread th = Thread.CurrentThread;
	
	//Setting the name of the thread	
	//By using the Name property of Thread class
	th.Name = "Main Thread";


	//Getting the just given mame to the default created Main Thread
	//By using the Name property of Thread class
	Console.WriteLine("Name of the thread - "+ th.Name);
	
	
	Console.WriteLine("The {0} going to sleep for 5 seconds", th.Name);

	//Sleep() method may throw InterruptedException, hence a try-catch block is needed
	Thread.Sleep(5000); 
	
	Console.WriteLine("The {0} awakes and exits", th.Name);
}
}


Output is


Name of the thread - Main Thread
The Main Thread going to sleep for 5 seconds
The Main Thread awakes and exits


Program Analysis







Difference between Process and a Thread


A process is simply a program in execution. For example a WordPad program used being used to edit a document is a process. There could be multiple processes running in a computer at the same time, for example- Media Player playing a movie, A text editor editing a document etc.

Whereas, a thread is a part of a program that is running concurrently with other parts of the program. For example, when you are using the WordPad program, you can edit a document and can also print another document at the same time. This is only possible because of multiple threads in execution at the same time in WordPad program, where one thread is looking after editing the document and other thread looking after printing a document.



Please share this article -





< Prev
Next >
< C# Serialization and Deserialization
C# Lifecycle of a Thread >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  Decodejava Twitter Page DecodeC# Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement