Home > DeveloperSection > Articles > Join, Sleep and Abort methods in C# Threading

Join, Sleep and Abort methods in C# Threading

Posted by  Arun Singh
on    August-27-2011 20:42 PM

C# C# 
Ratings:
0 Comment(s)
 7121  View(s)
Rate this:

Join, Sleep and Abort methods in C# Threading

There are lots of methods in Thread class. Here I am discussing about three main methods which is widely used in multithreading. Join method are used to calling threads and execute it  until thread not terminate, i.e Join method waits for finishing other threads by calling its join method. 

Sleep methods are generally used for suspends thread for specified time and after that time it will notify automatically in process. Abort method are used to terminate the threads.

Example:  

To use these method use System.Threading namespace and writes the following code.

using System.Threading;

 

namespace ThreadTest

{

    class Program

    {

        static voidMain(string[] args)

        {

            // Creating thread object to strat it

            Thread threadB= newThread(ThreadB);

            Thread threadC= newThread(ThreadC);

            Console.WriteLine("Threads started :");

            // Assign thread name

            threadB.Name= "Child thread";

            // Start thread B

            threadB.Start();

             threadC.Start();

            // join the Second thread after finishing the first thread

             threadB.Join();

              

            //Assign main thread name

            Thread.CurrentThread.Name= "Main Thread";

            //Thread A executes 10 times

            for (inti=0; i<10; i++)

            {

                if (i==4)

                    // Join thread c 

                    threadC.Join();

                if (i==5)

                {

                    // Terminate thread C

                    threadC.Abort();

                }

                Console.WriteLine(Thread.CurrentThread.Name);

                Thread.Sleep(100);

            }

         

            Console.WriteLine("Threads completed");

            Console.ReadKey();

        }

        public staticvoidThreadB()

        {

           // Executes thread B 10 times

          for(inti=0;i<10;i++)

          {

              // Suspends thread for 2 seconds

              Thread.Sleep(200);

             Console.WriteLine("Thread B");

          }

        }

        public staticvoidThreadC()

        {

            // Executes thread B 10 times

            for (inti=0; i<20; i++)

            {

                // Suspends thread for 2 seconds

                Thread.Sleep(200);

                Console.WriteLine("Thread C");

            }

        }

    }

}

 

Output:

Join, Sleep and Abort methods in C# Threading


Recent Activities

Manoj Pandey added new Article Fragment Life cycle in Android   4 hours ago

Kamlakar Singh added new Question Explain what is scope in Angular.js ?   5 hours ago

Madhu Mitha Created New Discussion how to save a shape as bmp file   8 hours ago


Don't want to miss updates? Please click the below button!