First of all I deeply thank you
C-SharpCorner.com for giving the best platform to sharpen our knowledge. It's amazing for every type of developer, beginner and advanced. Now, getting to my point.
The following picture is the best example of multithreading. In the following picture it is clear that one man is doing a couple of things at once.
Sr. Number |
Work |
1 |
Talking on Telephone Line |
2 |
Typing on keyboard |
3 |
Watching TV |
4 |
One leg playing the piano. |
Threading
Threading is a single process divided into multiple processes. In a simple way we can say a process is broken into multiple parts (tiny processes). For example:
Process |
Work |
1 |
Writing on www.c-sharpcorner.com |
2 |
Play the song on windows Media player |
3 |
Download movie using torrent |
4 |
Update the Windows |
5 |
Chat with friends on Google Talk |
Many examples exist in our real life.
Process |
Work |
1 |
Ride the Bike |
2 |
Sing a song |
3 |
Think about Exam. |
The following describes threading with an example.
The following shows how to start multi-threading. The Temp array holds the first or last name split. The following code first checks before starting the thread and if the thread is running then we can't start it again.
- Thread th1, th2; // Reference create
- ThreadStart ths1; //Reference Create
- private void button1_Click(object sender, EventArgs e)
- {
- string[] temp = First_lastName.Split(' ');
- firstName = temp[0];
- LastName = temp[1];
-
- Again:
- if (dtr < DateTime.Now)
- {
- th1.Priority = ThreadPriority.Highest;
- th2.Priority = ThreadPriority.BelowNormal;
-
- button1.Visible = false;
- if (th1.ThreadState != ThreadState.Running || th1.ThreadState!=ThreadState.WaitSleepJoin)
- {
- th1.Start();
- th2.Start();
- }
- else
- MessageBox.Show("your Thread Allready Runnning");
-
- }
- else
- {
- MessageBox.Show("wait");
- goto Again;
- }
- }
In this example is fun() and funn().
- public void funn()
- {
- LastName += "j";
- bool temp = false;
- again2:
- if (temp)
- {
- for (int i = 1; i <= LastName.Count() - 1; i++)
- {
- listBox2.Items.Add(LastName.Remove(i));
- Thread.Sleep(1000);
- }
- temp = false;
- }
- else
- {
- for (int i = LastName.Count() - 1; i > 0; i--)
- {
- listBox2.Items.Add(LastName.Remove(i));
- Thread.Sleep(1000);
- }
- temp = true;
-
- }
- goto again2;
-
-
- }
- public void fun()
- {
- firstName += "j";
- bool temp = false;
- again2:
- if (temp)
- {
- for (int i = 1; i <= firstName.Count() - 1; i++)
- {
- listBox1.Items.Add(firstName.Remove(i));
- Thread.Sleep(1000);
- }
- temp = false;
- }
- else
- {
- for (int i = firstName.Count() - 1; i > 0; i--)
- {
- listBox1.Items.Add(firstName.Remove(i));
- Thread.Sleep(1000);
- }
- temp = true;
-
- }
- goto again2;
-
- }
In the following picture we can see two listboxes that have a suspended thread.
- private void Tone_Normal_Click(object sender, EventArgs e)
- {
- if (th1.ThreadState == ThreadState.Suspended || th1.ThreadState == ThreadState.Aborted || th1.ThreadState == ThreadState.Stopped)
- MessageBox.Show("your thread is not live");
- else
- th1.Priority = ThreadPriority.Normal;
- }
- private void ThOneResume_Click(object sender, EventArgs e)
- {
- if (th1.ThreadState == ThreadState.Suspended || th1.ThreadState==ThreadState.Stopped)
- th1.Resume();
- else
- MessageBox.Show("Thread is not user-suspended; it cannot be resumed.");
- }
I hope everyone is clear about threading.