Threading Step by Step, Resume, Suspend, Abort

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.
 
 

Namespace Required    
  1. Using System.Threading;  
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. 
  1. Thread th1, th2;   // Reference create
  2. ThreadStart ths1;  //Reference Create 
  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             string[] temp = First_lastName.Split(' ');  
  4.             firstName = temp[0];  
  5.             LastName = temp[1];  
  6.   
  7.             Again:  
  8.             if (dtr < DateTime.Now)  
  9.             {  
  10.                 th1.Priority = ThreadPriority.Highest;  
  11.                 th2.Priority = ThreadPriority.BelowNormal;  
  12.   
  13.                 button1.Visible = false;  
  14.                 if (th1.ThreadState != ThreadState.Running || th1.ThreadState!=ThreadState.WaitSleepJoin)  
  15.                 {  
  16.                     th1.Start();  
  17.                     th2.Start();  
  18.                 }  
  19.                 else  
  20.                     MessageBox.Show("your Thread Allready Runnning");  
  21.   
  22.             }  
  23.             else  
  24.             {  
  25.                 MessageBox.Show("wait");  
  26.                 goto Again;  
  27.             }  
  28. }  
   
 
In this example is fun() and funn().  
  1. public void funn()  
  2.        {  
  3.            LastName += "j";  
  4.            bool temp = false;  
  5.            again2:  
  6.            if (temp)  
  7.            {  
  8.                for (int i = 1; i <= LastName.Count() - 1; i++)  
  9.                {  
  10.                    listBox2.Items.Add(LastName.Remove(i));  
  11.                    Thread.Sleep(1000);  
  12.                }  
  13.                temp = false;  
  14.            }  
  15.            else  
  16.            {  
  17.                for (int i = LastName.Count() - 1; i > 0; i--)  
  18.                {  
  19.                    listBox2.Items.Add(LastName.Remove(i));  
  20.                    Thread.Sleep(1000);  
  21.                }  
  22.                temp = true;  
  23.   
  24.            }  
  25.            goto again2;  
  26.               
  27.             
  28.        }  
  29.        public void fun()  
  30.        {  
  31.            firstName += "j";  
  32.            bool temp = false;  
  33.        again2:  
  34.            if (temp)  
  35.            {  
  36.                for (int i = 1; i <= firstName.Count() - 1; i++)  
  37.                {  
  38.                    listBox1.Items.Add(firstName.Remove(i));  
  39.                    Thread.Sleep(1000);  
  40.                }  
  41.                temp = false;  
  42.            }  
  43.            else  
  44.            {  
  45.                for (int i = firstName.Count() - 1; i > 0; i--)  
  46.                {  
  47.                    listBox1.Items.Add(firstName.Remove(i));  
  48.                    Thread.Sleep(1000);  
  49.                }  
  50.                temp = true;  
  51.   
  52.            }  
  53.            goto again2;  
  54.            
  55.        }  
In the following picture we can see two listboxes that have a suspended thread.  
  
  1. private void Tone_Normal_Click(object sender, EventArgs e)  
  2.       {  
  3.           if (th1.ThreadState == ThreadState.Suspended || th1.ThreadState == ThreadState.Aborted || th1.ThreadState == ThreadState.Stopped)  
  4.               MessageBox.Show("your thread is not live");  
  5.           else  
  6.               th1.Priority = ThreadPriority.Normal;  
  7.       }  
  8.       private void ThOneResume_Click(object sender, EventArgs e)  
  9.       {  
  10.           if (th1.ThreadState == ThreadState.Suspended || th1.ThreadState==ThreadState.Stopped)  
  11.               th1.Resume();  
  12.           else  
  13.               MessageBox.Show("Thread is not user-suspended; it cannot be resumed.");  
  14.       }  
I hope everyone is clear about threading.

Up Next
    Ebook Download
    View all
    Learn
    View all