multitreading in c# |
MULTITHREADING IN C#
“Multiple threading is a process
of using multiple threads in a project to execute different methods separately
without waiting for one another”
Basically by default all
applications are single threaded model in which only one thread or only main thread
executes all the instruction in sequence. If any problem such as server
response or data base response delay due to any reason, then the entire program
wait until the response without executing the rest of lines or method our
program stay stuck in a method without executing the other methods. Therefore
we use multiple threading in project to execute different method separately, if
one method stuck for a while then all the rest method will execute without
wasting time.
Syntax of Multithreading
using System.Threading;
Main
function
{
Thread t1 = new Thread(METHOD NMAE);
Thread t2 = new Thread(METHOD NAME);
Thread t3 = new Thread(METHOD NAME);
t1.Start();
t2.Start();
t3.Start();
}
First of all we have to include the System . Threading directive in our project after then we will instantiate the
instance of Thread and will pass the method name as parameter that should
execute through that thread, if the methods are non-static then in parameter
section we also have to specify the instance of class in that class the
specific method defines. At last we explicitly start the thread. See the
example for more deep.
EXAMPLE OF MULTITHREADING IN C#
using System.Threading;
namespace MultithreadingDemo
{
static class Muliplethread
{
public static void Testmethod1()
{
Console.WriteLine("Thread 1
starting");
for (int i=1;i<100;i++)
Console.WriteLine("Testmethod1:
line no" + i);
Console.WriteLine("Thread 1
exiting");
}
public static void Testmethod2()
{
Console.WriteLine("Thread 2
starting");
for (int i = 1; i < 100; i++)
Console.WriteLine("Testmethod2:
line no" + i);
Console.WriteLine("Thread 2
exiting");
}
public static void Testmethod3()
{
Console.WriteLine("Thread 3
starting");
for (int i = 1; i < 100; i++)
Console.WriteLine("Testmethod3:
line no" + i);
Console.WriteLine("Thread 3
exiting");
}
static void Main()
{
Console.WriteLine("Main thread
starting");
Thread t1 = new Thread(Testmethod1);
Thread t2 = new Thread(Testmethod2);
Thread t3 = new Thread(Testmethod3);
t1.Start(); t2.Start(); t3.Start();
Console.WriteLine("Main thread
exiting");
Console.ReadKey();
}
}
}
Quick overview of single threaded model and multithreaded model programs
Single threaded model
|
Multithreaded model
|
Single threaded model program are those
program in which only Main thread execute the entire program/application.
|
In multithreaded model program Main thread
makes multiple child threads to execute the program/application.
|
If any problem rise in any method for a
while. The time to execute the rest method will increase. So total time of
execution also increased.
|
Due to multiple threads we can save lots of
time if any problem rises in any method for a while.
|
.
0 comments:
Post a Comment
Please do not enter any spam link in the comment box