Monday 15 February 2021

Part 1 : Threading in C#

Before starting multi-threading, lets understand multitasking concept.
Multitasking is nothing but performing multiple tasks at same time. CPU handles multiple tasks at a time based of availability of core(CPU). Multiple applications(processes) are running on operating system like VLC media player, word document, browser and so on. For each application there is separate process defined and we can check it on Task manager window. Application (VLC, Browser, Word,…) opened on OS has different process and executed on CPU simultaneously.
Multithreading: Thread is noting but an executable helps to execute application code (piece of code written) to achieve some output like console application. Thread is small process executing piece of code and all applications (console, WPF, …etc) are by default single threaded application. All application comes with Main thread to execute logic written in an application, later for better performance programmer can add many background threads for better performance. Managing multiple threads for an application is nothing multithreading.
A console application in C# uses single thread by default, lets understand with example.
Current thread executing Console application with ManagedThreadId is 1. It means for all applications like console, asp.net Core, WPF application and so on are by default single threaded application.
Later to achieve better application performance you can add more background thread. In below sample I have created separate thread for PrintEvenNumber method which perform printing of even numbers for application
In below section, I have shown you how to call parameterized method using background thread. You can either use ParameterizedThreadStart class constructor to pass method name and instance of ParameterizedThreadStart provided to Thread constructor shown below.
object a = 10;
//Part 1 : Calling parameterized method for background thread
ParameterizedThreadStart param = new ParameterizedThreadStart(PrintEvenNumbers);
Thread t1 = new Thread(param);
t1.Start(a);
  Or normal calling of thread without creating instance of ParameterizedThreadStart and compiler will do it for you in background. //Part 2: Calling parameterized method for background thread
a = 20;
Thread t2 = new Thread(PrintEvenNumbers);// If ParameterizedThreadStart is not mentioned then compiler will do automatically in background
t2.Start(a);
In below example, I have passed input 100 to ‘Thread Even Number 1’ thread and 20 to ‘Thread Even Number 2’. Now I want Thread 2 wait till thread 1 finish execution. But in below screen in command prompt output, Thread Even Number 1 execute partially and control pass to Thread Even Number2 and start executing this thread.
  In real time in some situation, we usually avoid execution of two thread execute simultaneously. To achieve this need to use Join method in C#. Check below screen of command prompt it wait till Thread 1 execute and start execution of Thread 2 after Thread 1 finished.

1 Comments:

At 7 March 2021 at 04:07 , Blogger vilas said...

Good, short and sweet.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home