Hi ,
In the last post I discussed about what multi-threading is all about.
Please refer to "Understanding Multi-Threading in C#" in case you need to know about it.
This blog is all about the implementation of the same.
Too implement Multi-threading we make use of Thread class which is present in "System.Threading" namespace.
To make a portion of code to be implemented in separate thread(also known as worker thread) we need to have that chunk of code in another method.
For example:
If our existing program looks like this:
class Program
{
public static void Main()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("Rest of the code");
}
}
then in order to make use of another thread we need to have an extra method which will run as the thread.
so the above code will be changed to :
class Program
{
public static void Main()
{
ForMyNewThread(); //just a dummy name;u may give ur pets name too ;)
Console.WriteLine("Rest of the code");
}
private static void ForMyNewThread()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
}
Now, we need to pass this method(ForMyNewThread) to the constructor of the Thread class, so that our method can act like a Thread.
Note: Please don't forget to include System.Threading name space;
so our code looks like this now:
public static void Main()
{
Thread Mythread = new Thread(ForMyNewThread);
Mythread.Start();
Console.WriteLine("Rest of the code");
}
private static void ForMyNewThread()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
"Start()" is used to make the Thread functional,if we don't give start the thread will not get started and the our program will return just "Rest of the code".
I strongly recommend you to try the above example one to have a better understanding.
If you actually try the above example(which u should :p ), u may see that the constructor of the Thread class has four overloads, which expect either "ThreadStart" or "ParameterisedThreadStart".
So what are these and why we got the output without passing the above in our previous example!!???
Magic!! Nah doesn't happen in real life! :-P
Actually "ThreadStart" and "ParameterisedThreadStart" are delegates.
If you don't know what are delegates, then it is advised to see my post named,"Real Use Of Delegates", where I have explained properly what and why should we use Delegates.
So coming back to our current topic :p ;)
Now as we know thread is basically a method which will have a chunk of code in it which will be executed when thread.start() is invoked.
but how do we point a method to thread!! Please note that i wrote "point a method"!!..can you recollect something!!? ..Yes you are right a Delegate(Function pointer!).So a Thread class uses Delegate to point to a particular method.
But how did it work before !!
we didn't passed any delegate there, it is because .Net framework is doing it by itself!!(cool right!, at least if you are a lazy person like me you would love this feature of .Net framework )
Program with the delegate:
the output remains the same.
Wondering what is "ParameterizedThreadStart delegate"!!?
If we want to pass some data to the method we make use of ParameterizedThreadStart.
class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number for which you want to see all the natural numbers less than or equal to the number");
object target = Console.ReadLine();
Thread T1 = new Thread(new ParameterizedThreadStart(ForMyNewThread));
T1.Start(target);
}
public static void ForMyNewThread(object target)
{
int numberEnteredByUser = 0;
if (int.TryParse(target.ToString(), out numberEnteredByUser))
{
for (int i = 1; i <= numberEnteredByUser; i++)
{
Console.WriteLine(i);
}
}
}
}
Here in this program we are prompting the user to enter a number of his choice and we will display all the natural numbers less than or equal to the Entered number.
As you may see we can pass the data in Start() method itself.
Hope This is clear.
Do leave your comments if you need any clarification.
Regards,
Ashish Agarwal
In the last post I discussed about what multi-threading is all about.
Please refer to "Understanding Multi-Threading in C#" in case you need to know about it.
This blog is all about the implementation of the same.
Too implement Multi-threading we make use of Thread class which is present in "System.Threading" namespace.
To make a portion of code to be implemented in separate thread(also known as worker thread) we need to have that chunk of code in another method.
For example:
If our existing program looks like this:
class Program
{
public static void Main()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("Rest of the code");
}
}
so the above code will be changed to :
class Program
{
public static void Main()
{
ForMyNewThread(); //just a dummy name;u may give ur pets name too ;)
Console.WriteLine("Rest of the code");
}
private static void ForMyNewThread()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
}
Now, we need to pass this method(ForMyNewThread) to the constructor of the Thread class, so that our method can act like a Thread.
Note: Please don't forget to include System.Threading name space;
so our code looks like this now:
public static void Main()
{
Thread Mythread = new Thread(ForMyNewThread);
Mythread.Start();
Console.WriteLine("Rest of the code");
}
private static void ForMyNewThread()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
"Start()" is used to make the Thread functional,if we don't give start the thread will not get started and the our program will return just "Rest of the code".
I strongly recommend you to try the above example one to have a better understanding.
If you actually try the above example(which u should :p ), u may see that the constructor of the Thread class has four overloads, which expect either "ThreadStart" or "ParameterisedThreadStart".
So what are these and why we got the output without passing the above in our previous example!!???
Magic!! Nah doesn't happen in real life! :-P
Actually "ThreadStart" and "ParameterisedThreadStart" are delegates.
If you don't know what are delegates, then it is advised to see my post named,"Real Use Of Delegates", where I have explained properly what and why should we use Delegates.
So coming back to our current topic :p ;)
Now as we know thread is basically a method which will have a chunk of code in it which will be executed when thread.start() is invoked.
but how do we point a method to thread!! Please note that i wrote "point a method"!!..can you recollect something!!? ..Yes you are right a Delegate(Function pointer!).So a Thread class uses Delegate to point to a particular method.
But how did it work before !!
we didn't passed any delegate there, it is because .Net framework is doing it by itself!!(cool right!, at least if you are a lazy person like me you would love this feature of .Net framework )
Program with the delegate:
public static void Main()
{
Console.WriteLine("Rest of the code");
Thread Mythread = new Thread(new ThreadStart(ForMyNewThread));
Mythread.Start();
}
private static void ForMyNewThread()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
the output remains the same.
Wondering what is "ParameterizedThreadStart delegate"!!?
If we want to pass some data to the method we make use of ParameterizedThreadStart.
class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number for which you want to see all the natural numbers less than or equal to the number");
object target = Console.ReadLine();
Thread T1 = new Thread(new ParameterizedThreadStart(ForMyNewThread));
T1.Start(target);
}
public static void ForMyNewThread(object target)
{
int numberEnteredByUser = 0;
if (int.TryParse(target.ToString(), out numberEnteredByUser))
{
for (int i = 1; i <= numberEnteredByUser; i++)
{
Console.WriteLine(i);
}
}
}
}
Here in this program we are prompting the user to enter a number of his choice and we will display all the natural numbers less than or equal to the Entered number.
As you may see we can pass the data in Start() method itself.
Hope This is clear.
Do leave your comments if you need any clarification.
Regards,
Ashish Agarwal