none
Threading RRS feed

  • Question

  • I have created threads dynamically.

    static void Main(string[] args)
    {
        int ThreadCount =Convert.ToInt32ConfigurationManager.AppSettings["Threads"]);
        List<Thread> th = new List<Thread>();
        for (int i = 0; i < ThreadCount; i++)
        {
           Thread t = new Thread(print);
           th.Add(t);
        }
        foreach (Thread t in th)
        {
            t.Start();
        }
    }
    public void print()
    {
        console.writeline("123");
    }

    I want to know when this threads will complete.
    On completion of these threads i want to print a message of "DONE"

    How can i do this.


    markand

    Monday, October 29, 2012 2:21 PM

Answers

  • Why don't you just use Join?

    static void Main(string[] args)
    {
        int ThreadCount = Convert.ToInt32ConfigurationManager.AppSettings["Threads"]);
        List<Thread> th = new List<Thread>();
        for (int i = 0; i < ThreadCount; i++)
        {
           Thread t = new Thread(print);
           th.Add(t);
        }
        
        foreach (Thread t in th)
        {
            t.Start();
            t.Join();   //Wait until this thread finishes
        }
    
        Console.WriteLine("DONE");
    }
    
    public void print()
    {
        console.writeline("123");
    }

    On the other hand, if you are using .NET 4.0, then you can consider CountdownEvent,

    static CountdownEvent;
    static void Main(string[] args)
    {
        int ThreadCount = Convert.ToInt32ConfigurationManager.AppSettings["Threads"]);
        countdown = new CountdownEvent (ThreadCount); //----> Create Object
        List<Thread> th = new List<Thread>();
        for (int i = 0; i < ThreadCount; i++)
        {
           Thread t = new Thread(print);
           th.Add(t);
        }
        
        foreach (Thread t in th)
        {
            t.Start();
        }
    
        countdown.Wait(); //Wait until all threads complete
    
        Console.WriteLine("DONE");
    }
    
    public void print()
    {
        console.writeline("123");
        countdown.Signal();  //Send completion signal to Main
    }

    I hope this helps,


    Please mark this post as answer if it solved your problem. Happy Programming!


    • Edited by Adavesh Monday, October 29, 2012 4:25 PM
    • Marked as answer by Mike FengModerator Wednesday, December 12, 2012 5:53 AM
    Monday, October 29, 2012 4:07 PM
  • You can use the IsAlive property to determine if it is still running, but that's not ideal, as you would have to periodically poll the property.

    Personally, I would call a function on end.  Specifically, your print() method would call a ThreadCompleted() method, passing the minimum amount of arguments needed to properly identify the thread and the work that was being done.  If you need to know elsewhere if the thread is still running, you could save the System.Threading.Thread objects for later use or your could create boolean flags of your own.


    Jose R. MCP
    Code Samples

    Monday, October 29, 2012 2:32 PM

All replies

  • You can use the IsAlive property to determine if it is still running, but that's not ideal, as you would have to periodically poll the property.

    Personally, I would call a function on end.  Specifically, your print() method would call a ThreadCompleted() method, passing the minimum amount of arguments needed to properly identify the thread and the work that was being done.  If you need to know elsewhere if the thread is still running, you could save the System.Threading.Thread objects for later use or your could create boolean flags of your own.


    Jose R. MCP
    Code Samples

    Monday, October 29, 2012 2:32 PM
  • Why don't you just use Join?

    static void Main(string[] args)
    {
        int ThreadCount = Convert.ToInt32ConfigurationManager.AppSettings["Threads"]);
        List<Thread> th = new List<Thread>();
        for (int i = 0; i < ThreadCount; i++)
        {
           Thread t = new Thread(print);
           th.Add(t);
        }
        
        foreach (Thread t in th)
        {
            t.Start();
            t.Join();   //Wait until this thread finishes
        }
    
        Console.WriteLine("DONE");
    }
    
    public void print()
    {
        console.writeline("123");
    }

    On the other hand, if you are using .NET 4.0, then you can consider CountdownEvent,

    static CountdownEvent;
    static void Main(string[] args)
    {
        int ThreadCount = Convert.ToInt32ConfigurationManager.AppSettings["Threads"]);
        countdown = new CountdownEvent (ThreadCount); //----> Create Object
        List<Thread> th = new List<Thread>();
        for (int i = 0; i < ThreadCount; i++)
        {
           Thread t = new Thread(print);
           th.Add(t);
        }
        
        foreach (Thread t in th)
        {
            t.Start();
        }
    
        countdown.Wait(); //Wait until all threads complete
    
        Console.WriteLine("DONE");
    }
    
    public void print()
    {
        console.writeline("123");
        countdown.Signal();  //Send completion signal to Main
    }

    I hope this helps,


    Please mark this post as answer if it solved your problem. Happy Programming!


    • Edited by Adavesh Monday, October 29, 2012 4:25 PM
    • Marked as answer by Mike FengModerator Wednesday, December 12, 2012 5:53 AM
    Monday, October 29, 2012 4:07 PM
  • I didn't mention Join() because it goes one thread at a time.  It is more natural to me to simply have a callback from the worker function if all I need is notification.  If I also need synchronization, then yes, Join() is a candidate.  For a message to show, Join() is not really necessary.

    CountdownEvent on the other hand looks cool.  I was unaware of it. :-)


    Jose R. MCP
    Code Samples

    Monday, October 29, 2012 4:14 PM
  • Hi Markand,

    Welcome to the MSDN Forum.

    How about using threadpool rather than list?

    If so, you can try the GetAvailable method to get the available threads, if it is the max thread numbers, it means all works done.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, October 30, 2012 10:34 AM
    Moderator
  • I saw this question, and depending on your use case, you could consider using a Parallel.For:

    //Pasted from Linqpad http://www.linqpad.com 
    void Main() 
    { 
        int ThreadCount = 20; //import from args if you like.     
        Parallel.For(0, ThreadCount, x =>
                                         { 
                                             System.Threading.Thread.Sleep(2); //Added to simulate some parallel work
                                             Console.WriteLine("123"); 
                                         }); 
        Console.WriteLine("Done"); 
    }

    MSDN Library entry for Parallel.For



    Tuesday, October 30, 2012 4:47 PM