Multi Threading in C# .Net

Ratings:
(4)
Views: 673
Banner-Img
Share this blog:

 

Multithreading

Screenshot_72  

  1. Multi means many

Thread means process

  1. The process indicates either a part of a program or a complete program also
  2. Multithreading is the concept of executing more than one process simultaneously.
  3. Multithreading is used to develop parallel processing

Pentium iv 4.3 GHZà43 Lakhs of instructions(cycle ) within 1min Speed

  1. Sharing CPU cycles efficiently is also called as “Multi-Threading”.
  2. The operating system allocates a set of as cles based on the priority(importance)of the process.

Ctrl+ Alt+ del

  1. Based on above statement, the os are divided into two types:-

Interested in mastering .Net Training? Enroll now for a FREE demo on ".Net Training"

Single-Threaded

DOS UNIX

  Screenshot_74  

given to one app only     

Multi-Threading

Screenshot_75    

  1. If o/s handles only one process at a time, then it is called as “single thread os”.
  2. Multi-thread os handles more than one process also
  3. To work with multi-threading, NET introduced

System. Threading namespace

  1. Classes of system. Threading namespace
  2. a) Thread: Used to maintain the life aside of the threads
  3. b) Thread start: Used to create the threads.
  4. c) Mutex: Used in thread synchronization

   

Thread life cycle:-

Screenshot_76    

Four steps for writing threading applications:_

  1. Write a class with methods

Class test {

Public vaid print(){}

}

Check Out .Net Tutorial

  1. Create an object for thread start delegate with the address of a function

            Syntax:-

Test t = new test();

Thread start ts=new thread start(t.print);

  1. Create a thread with the help of thread start object

Thread tr=new thread(ts);

Start the thread Tr. start()  

Example1:-

  Screenshot_1  

A program to execute two for loops simultaneously

Screenshot_24Open console application project Using system. Threading;

Screenshot_24code in GD Class test

{

Public static vold p1()

{

For (int i=1; i<=10;i++)

{

Console. write(I+””);

Thread . sleep(500);

}//for

}

//p1

Public static void p2()

{

For(int k=11;k<20;k++)

{

Console. write link(k);

Thread. sleep(500)

//500 millisec

}//for

}

//p2 }//test  

Code for the main method:-

Thread start ts1=new thread start (Test.p1)

Thread start ts2=new thread start (Test.p2)

Thread t1=new thread (ts1)

Thread t2=new thread (ts1):

//here

ts1 T1.start() T2.start() Console.Read key();

T1.suspend(); Console. Read key();

T1.Resume(); Console. Read key ();

o/p : 112233----2020  

Thread synchronization:-

Screenshot_2  

1.Thread synchronization is the concept of allowing only one thread at a time into one process

2.It process is busy, threads need to wait in the queue

3.Waiting threads will be executed based on priority

  1. Mutex is a class which controls waiting for threads with the help of wait one() and release mutex() 

  OBS:

Screenshot_3  

5.The code which is written in between wait one()and release mutex()is called a synchronized code.  

A Program to print ”ALL THE BEST”3 TIMES WITH THE HELP OF THREADING:

Screenshot_4

{

Console. write(“ALL”);

Console. write(“THE”);

Console. write(“Best”);

}

Screenshot_24open console application project Using system. Threading;    

Code in GD

Class program

{

Static mutex m = new mutex();

Class test

{

Public static void print()

{

wait one();
console. write(“ALL”);

Thread. sleep(1000);

Consote. write(“THE”);

Thread .sleep(1000);

Console. write(“BEST”);

Thread. sleep(1000);

Console. Write line();

//for new line Thread. sleep(1000);

Release mutex();
}//print }//test  

Code for the main method:-

{

Thread start ts=new thread start(test. print);

Thread start t1=new thread (ts);

Thread start t2=new thread (ts);

Thread start t3=new thread (ts);

T1.Start(); T2.Start();

T3.Start();

Console. Read key();

}  

Pointers:-

Screenshot_5

Why we use pointers? For fast accessing Fig(a) Accessing ’i’ value is fast when compared to the pointer variable, but it is not in this case fast. Char*p                               

2B Double*p                        

Screenshot_24Logic is m/c of o/s they are unmanaged code.

Screenshot_24The size of the pointers varies or dependent on the o/s, so they are unmanaged code, so c#.net only supports pointers but not implemented.

  1. Pointer holds the address of a variable.
  2. The pointer is unmanaged code in c#.net.
  3. The pointer is also called an unsafe code.

Unsafe is a keyword Pointers logic must be the part of the unsafe block only Pointer size varies from o/s to o/s Generally o/s are divided into 3 types Screenshot_6  

Unmanaged code is not all recommended In c# .net it we want to use in c# by using un sate keyword

Ex:-  

Place a button on the form

{

Un sate { Int iz 90; Int*p; P=&I;

Int k; K=*p**p; MBS(K+””);

}

By pressing f5 it gives a warning that you are using un state code. Please inform to CLR to execute unsafe code.

Go to project

Screenshot_24properties

Screenshot_24buildàCheck Menu “Allow un sate code” Check Box Execute the project Inter+ view

Screenshot_24 Internal look-Behavior

Screenshot_24subject

Screenshot_24time sense

Screenshot_24Body language

Screenshot_24confidence          

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox