Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

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


by Arun Singh on 8/27/2011 8:42:40 PM

Views: 5364       Comments: 0

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 void Main(string[] args)

        {

            // Creating thread object to strat it

            Thread threadB = new Thread(ThreadB);

            Thread threadC = new Thread(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 (int i = 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 static void ThreadB()

        {

           // Executes thread B 10 times

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

          {

              // Suspends thread for 2 seconds

              Thread.Sleep(200);

             Console.WriteLine("Thread B");

          }

        }

        public static void ThreadC()

        {

            // Executes thread B 10 times

            for (int i = 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

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5091
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.