Group Discounts available for 3+ students and Corporate Clients

Multi Threading in C# .Net

Screenshot_72

 

  1. Multi means many

Thread means process

  1. Process indicates either a part of a program or a complete program also
  2. Multi threading is a concept of executing more than one process simultaneously.
  3. Multi threading 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. 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:-

 

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 name space

  1. Classes of system. Threading name space
  2. a) Thread: Used to maintain the life asele 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(){}

}

  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 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 prlority

  1. Mutex is a class which controls waiting 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 as 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()

{

  1. 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);

  1. Release mutex();

}//print

}//test

 

Code for 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)

Accesing ’i’ value is fast when compared to pointer variable, but it is not in this case fast.

Char*p                                2B

Double*p                          2B

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. Pointer are unmanaged code in c#.net.
  3. Pointer are also called as unsafe code.

Unsafe is a key word

Pointers logic must be the part of unsafe block only

Pointer size varies from o/s to o/s

Generally o/s are divided into 3 types

Screenshot_6

 

Un managed code is not all recommended In c# .net it we want to use in c# by using un sate key word

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 if gives warning that you are using un state code. Please in form to CLR to execute unsafe code.

Go to project Screenshot_24propertiesScreenshot_24buildàCheck

Menu

“Allow un sate code”

Check Box

Execute the project

Inter+ viewScreenshot_24 Internal look-Behavior

Screenshot_24subject

Screenshot_24time sense

Screenshot_24Body language

Screenshot_24confidence

 

 

 

 

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Multi Threading in C# .Net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please Enter Your Details and Query.
    Three + 6