views:

146

answers:

7

Hi, how to make the foreground thread wait for all background (child) threads to finish in C#? I need to get list of pending jobs from the queue (database), start a new thread to execute each of them and finally wait for all the child threads to finish. how to do that in C#? Thanks in advance.

+2  A: 

Look into the Thread.Join() method. The calling thread will be paused while it waits for the thread passed in as a parameter.

If you keep a reference to all the threads that you create with new Thread() then after you have looped through them all and started them, loop through again and call Join() on each.

This is a very simplistic and possibly crude way to solve the problem though. Blocking the main thread might cause bad (user?) experience if related to a user-interface or even make your program unresponsive. If you add more context to the question, you might get better tailored answers. Possibly you need a callback mechanism from the background threads so that you know when they've each finished.

Neil Fenwick
+5  A: 

You could store each launched thread in an array. Then when you need to wait for them all, call Join method on each thread in an array in a loop.

Thread child = new Thread(...);
Threads.Add(child);
child.Start()

...

foreach(Thread t in Threads)
{
   t.Join();
}

HTH

Armen Tsirunyan
Thanks for the reply. I think thread.Join method is good for a single thread or very few fixed number of threads. for multiple threads, I think there is a WaitAll method, but I couldn't find a good code example for it.
RKP
@RKP: Waiting fo all threads to finish is the same as to wait for them in turn, isn't it? There is a winapi function WaitForMultipleObjects, but it's not C#, though you can use it, but I see no point.
Armen Tsirunyan
A: 

Use ManualResetEvent

saurabh
+1  A: 

This is incomplete code, but ManualResetEvent works for you

        var waitEvents = new List<ManualResetEvent>();
        foreach (var action in actions)
        {
            var evt = new ManualResetEvent(false);
            waitEvents.Add(evt);
            ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, TimeoutCallback, state, 5000, true);
        }


        if (waitEvents.Count > 0)
            WaitHandle.WaitAll(waitEvents.ToArray());
danijels
Thanks, but where the new thread for each action is started here in this code? is "5000" a timeout setting?
RKP
Look here for entire code, I just copied a bit from one of my previous answers: http://stackoverflow.com/questions/3915017/image-url-validation-in-c/3915440#3915440
danijels
And yes, 5000 was a timeout setting
danijels
A: 

have u try EventWaitHandle();

888
A: 

Create a structure to keep track of your worker threads

            private struct WorkerThreadElement
            {
                public IAsyncResult WorkerThreadResult;
                public AsyncActionExecution WorkerThread;
            }

You also need to keep track the total number of threads expected to be created and the number of threads that have currently completed

            private int _TotalThreads = 0;
            private int _ThreadsHandled = 0;
            private List<WorkerThreadElement> _WorkerThreadElements = new List<WorkerThreadElement>();

Then create an autoreset handle in order to wait for thread completion.

            // The wait handle thread construct to signal the completion of this process
            private EventWaitHandle _CompletedHandle = new AutoResetEvent(false);

You also need a delegate to create new threads - There are multiple ways of doing this but i have chosen a simple delegate for the sake of this example

            // Delegate to asynchronously invoke an action
            private delegate void AsyncActionExecution();

Lets asume that the Invoke method is the entrance point that will create all threads and wait for their execution. So we have:

            public void Invoke()
            { 
              _TotalThreads = N; /* Change with the total number of threads expected */

              foreach (Object o in objects) 
              {
                 this.InvokeOneThread();
              }             

              // Wait until execution has been completed
              _CompletedHandle.WaitOne();

              // Collect any exceptions thrown and bubble them up
              foreach (WorkerThreadElement workerThreadElement in _WorkerThreadElements)
              {                      workerThreadElement.WorkerThread.EndInvoke(workerThreadElement.WorkerThreadResult);
              }
            }

InvokeOneThread is the method used to create a single thread for one operation. Here we need to create a worker thread element and invoke the actual thread.

            private void InvokeOneThread()
            {
                WorkerThreadElement threadElement = new WorkerThreadElement();
                threadElement.WorkerThread = new AsyncActionExecution();
                threadElement.WorkerThreadResult = threadElement.WorkerThread.BeginInvoke(actionParameters, InvokationCompleted, null);

                _WorkerThreadElements.Add(threadElement);
            }

Callback from thread completion

            private object _RowLocker = new object();
            /// <summary>
            /// Increment the number of rows that have been fully processed
            /// </summary>
            /// <param name="ar"></param>
            private void InvokationCompleted(IAsyncResult ar)
            {
                lock (_RowLocker) { _RowsHandled++; }

                if (_TotalThreads == _ThreadsHandled) _CompletedHandle.Set();
            }

Done

Yannis
A: 

Consider using ThreadPool. Most of what you want is already done. There is an example from Microsoft which does pretty much your entire task. Replace "fibonacci" with "database task" and it sounds like your problem.

plinth
I read this article few minutes back and was about to reply saying that is the solution I was looking for. thanks for the reply.
RKP