How to know when all threads using ThreadWorker class have completed

I have an issue with an application that loads a lot of data into memory and then keeps it updated.
The pipe that we connect through is slow and so I have divided the TCP requests to load individual major components concurrently rather than sequentially.
public void SetAllMeetings(Boolean addEvents)
        {
            try
            {
                meetings = new List<MeetingDetails>();
                string request = Requests.GetGenericString(system, systemId, "Get_Meetings", "All", "", "");
                string response = requestStream.GetResponse(request);

                XmlDocument xml_doc = new XmlDocument();
                xml_doc.LoadXml(response);
                XmlNodeList meetingNodes = xml_doc.SelectNodes("Response/Meetings/Meeting");
                foreach (XmlNode meeting in meetingNodes)
                {
                    StartMeetingCreationThread(meeting);
                }   
            }
            catch (Exception ex)
            {
                ExceptionStruct temp = new ExceptionStruct(DateTime.Now, "RaceDay", ex.Message, ex.StackTrace.ToString());
                exceptionLogger.Add(temp);
            }
        }

public void StartMeetingCreationThread(XmlNode meeting)
        {
            MeetingCreationWorker worker = new MeetingCreationWorker(meeting, ref meetings, system, systemId, ref exceptionLogger);
            Thread thread = new Thread(worker.DoWork);
            thread.Priority = ThreadPriority.Normal;
            thread.IsBackground = true;
            thread.Start();
        }
 
public class MeetingCreationWorker : ThreadWorker

public override void DoWork()
        {
            try
            {
                MeetingDetails temp = new MeetingDetails(updateStream, system, systemId, meeting.SelectSingleNode("Meeting_Number").InnerText, ref exceptionLogger);   
                temp.SetEventDetails();     
                meetings.Add(temp);
            }
            catch (Exception ex)
            {
                ExceptionStruct temp = new ExceptionStruct(DateTime.Now, "RaceDay", ex.Message, ex.StackTrace.ToString());
                exceptionLogger.Add(temp);
            }
            finally
            {
                updateStream = null;
            }
        }

Open in new window

.

Each meeting now loads at the same time by creating a meeting object and all sub-objects.

How can I check that all the DoWork() procedures in the ThreadWorker object threads have finished and so the loading is complete from the main thread?

I had thought of creating a List<MeetingCreationWorker> and checking that they had completed using an internal variable. I would then, inside a loop, wait until every variable had been set to a value before moving on.
Does anyone have a better idea?

The MeetingCreationWorker objects and corresponding threads are discarded after the initial load.
LVL 1
jetbetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
I, personally, would use an event:
public void SetAllMeetings(Boolean addEvents)
{
	try
	{
		meetings = new List<MeetingDetails>();
		string request = Requests.GetGenericString(system, systemId, "Get_Meetings", "All", "", "");
		string response = requestStream.GetResponse(request);

		XmlDocument xml_doc = new XmlDocument();
		xml_doc.LoadXml(response);
		XmlNodeList meetingNodes = xml_doc.SelectNodes("Response/Meetings/Meeting");
		foreach (XmlNode meeting in meetingNodes)
		{
			StartMeetingCreationThread(meeting);
		}
	}
	catch (Exception ex)
	{
		ExceptionStruct temp = new ExceptionStruct(DateTime.Now, "RaceDay", ex.Message, ex.StackTrace.ToString());
		exceptionLogger.Add(temp);
	}
}

public void StartMeetingCreationThread(XmlNode meeting)
{
	MeetingCreationWorker worker = new MeetingCreationWorker(meeting, ref meetings, system, systemId, ref exceptionLogger);
	worker.OnThreadWorkerCompleted += ThreadWorkerCompleted;
	Thread thread = new Thread(worker.DoWork);
	thread.Priority = ThreadPriority.Normal;
	thread.IsBackground = true;
	thread.Start();
}

public void ThreadWorkerCompleted(object sender, ThreadWorkerCompletedEventArgs e)
{
	// Do something meaningful here...
}
 
public class MeetingCreationWorker : ThreadWorker
{
	#region ThreadWorderCompleted Memebers
	[NonSerialized]
	private ThreadWorkerCompletedEventHandler _threadWorkerCompleted;
	public event ThreadWorkerCompletedEventHandler ThreadWorkerCompleted
	{
		add { _threadWorkerCompleted += value; }
		remove { _threadWorkerCompleted -= value; }
	}

	protected virtual void OnThreadWorkerCompleted(object sender, ThreadWorkerCompletedEventArgs e)
	{
		ThreadWorkerCompletedEventHandler handler = _threadWorkerCompleted;
		if (handler != null)
			handler(sender, e);
	}
	#endregion

	public override void DoWork()
	{
		try
		{
			MeetingDetails temp = new MeetingDetails(updateStream, system, systemId, meeting.SelectSingleNode("Meeting_Number").InnerText, ref exceptionLogger);   
			temp.SetEventDetails();     
			meetings.Add(temp);
		}
		catch (Exception ex)
		{
			ExceptionStruct temp = new ExceptionStruct(DateTime.Now, "RaceDay", ex.Message, ex.StackTrace.ToString());
			exceptionLogger.Add(temp);
		}
		finally
		{
			updateStream = null;
		}
		OnThreadWorkerCompleted(this, new ThreadWorkerCompletedEventArgs(meeting, system, systemId));
	}
}

public delegate void ThreadWorkerCompletedEventHandler(object sender, ThreadWorkerCompletedEventArgs e);

public class ThreadWorkerCompletedEventArgs : EventArgs
{
	// Ideally you really want to use items that can identify the thread.
	public XmlNode Meeting ( get; private set; }
	// Just guessing that a system is a string
	public string System { get; private set; }
	// Just guessing that a systemId is an integer
	public int SystemID { get; private set; } 

	private ThreadWorkerCompletedEventArgs()
	{
		Meeting = default(XmlNode);
		SystemID = default(int);
		System = default(string);
	}

	public ThreadWorkerCompletedEventArgs(XmlNode Meeting, string System, int SystemID)
	{
		this.Meeting = Meeting;
		this.System = System;
		this.SystemID = SystemID;
	}
}

Open in new window


-saige-
0
it_saigeDeveloperCommented:
Ofcourse, before you implement your own Completed event, I would first check to see if the ThreadWorker class you are dealing with implements it's own Completed event.  You might find some other useful events that the class implements, i.e Progress, Canceled and such.

-saige-
0
jetbetAuthor Commented:
Thanks for your input.
The thread worker class is a construct suggested by MS that I use as a base class whenever I need a new Thread.

I have solved the issue using my original idea of adding a Boolean variable to the MeetingCreationWorker and inside the "finally" block of the DoWork() function that I can then Query using the following code on the main thread.

public void StartMeetingCreationThread(XmlNode meeting)
        {
            MeetingCreationWorker worker = new MeetingCreationWorker(meeting, ref meetings, system, systemId, showAllRunners, ref raceSportLinks, oddsStoreURL, oddsStoreTimeout, triggerEvents, JetbetDate, ref exceptionLogger);
            meetingCreationList.Add(worker);
            Thread thread = new Thread(worker.DoWork);
            thread.Priority = ThreadPriority.Normal;
            thread.IsBackground = true;
            thread.Start();
        }
// From main thread
....

foreach (XmlNode meeting in meetingNodes)
                {
                    StartMeetingCreationThread(meeting);
                }

                while (meetingCreationList.Count > 0)
                {
                    for (int i = meetingCreationList.Count - 1; i > -1; i--)
                    {
                        if (meetingCreationList[i].finished)
                        {
                            meetingCreationList.RemoveAt(i);
                        }
                    }
                    Thread.Sleep(1000);
                }

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jetbetAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for jetbet's comment #a40375930

for the following reason:

No better solutions were offered
0
it_saigeDeveloperCommented:
I understand that you used your original idea.  However, using an event to trigger the completion of a thread is more efficient then using a Thread.Sleep().

http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program/

-saige-
0
jetbetAuthor Commented:
That is a good point but the issue is that I do not want the process to move on until all Creation Threads have completed their task.
I may have misunderstood your answer on how you suggested this was to be achieved with waiting for the 20 or so triggers.
0
it_saigeDeveloperCommented:
Not a problem.  There are a couple of varying ways to handle waiting for the threads to complete.  One way is to use either a manual or auto reset event in conjunction with the completed event already suggested, the trick to this method is how to proplerly determine when the reset event would be signaled that all thread had completed.  One way would be to use a boolean value to indicate that you are creating threads and then when you are finished creating your threads, you set this boolean value to false to signal to the completed event that it is okay to start checking the for the existence of the remaining threads.  When there are no more threads to process and no more threads to add, the completed event would signal the reset event that it is okay to continue processing.  So lets put it all together:
using System;
using System.Collections.Generic;
using System.Threading;

namespace CSharpThreadExampleExample
{
	public class Worker
	{
		#region ThreadWorderCompleted Memebers
		[NonSerialized]
		private ThreadWorkerCompletedEventHandler _threadWorkerCompleted;
		public event ThreadWorkerCompletedEventHandler ThreadWorkerCompleted
		{
			add { _threadWorkerCompleted += value; }
			remove { _threadWorkerCompleted -= value; }
		}

		protected virtual void OnThreadWorkerCompleted(object sender, ThreadWorkerCompletedEventArgs e)
		{
			ThreadWorkerCompletedEventHandler handler = _threadWorkerCompleted;
			if (handler != null)
				handler(sender, e);
		}
		#endregion

		private bool shouldStop = false;
		private int id = -1;
		public int ID
		{
			get { return id; }
			private set 
			{ 
				if (!value.Equals(id))
					id = value;
			}
		}

		public void DoWork()
		{
			int count = 0;
			while (!shouldStop)
			{
				Console.WriteLine(string.Format("Worker Thread {0}: Working ({1} passes)...", ID, count + 1));
				// Simulating long running process.
				Thread.Sleep(200);
				count++;
				if (count == 5)
					shouldStop = true;
			}
			Console.WriteLine(string.Format("Worker Thread {0}: Terminating gracefully", ID));
			OnThreadWorkerCompleted(this, new ThreadWorkerCompletedEventArgs(ID));
		}

		public void RequestStop()
		{
			shouldStop = true;
		}

		private Worker() { ;}

		public Worker(int ID)
		{
			id = ID;
		}
	}

	class Program
	{
		static AutoResetEvent reset = new AutoResetEvent(false);
		static List<Worker> workers = new List<Worker>();
		static bool creatingThreads = false;

		static void Main(string[] args)
		{
			creatingThreads = true;
			for (int i = 0; i < 5; i++)
				CreateAndStartThreads(i + 1);
			Console.WriteLine("Settings the reset event to block the main thread");
			reset.WaitOne();
			Console.WriteLine("Finished executing all threads.");
			Console.ReadLine();
		}

		public static void CreateAndStartThreads(int ID)
		{
			Worker worker = new Worker(ID);
			worker.ThreadWorkerCompleted += OnThreadWorkerCompleted;
			Thread thread = new Thread(worker.DoWork) { Priority = ThreadPriority.Normal, IsBackground = true };
			thread.Start();
			Console.WriteLine(string.Format("Adding worker with ID - {0}", ID));
			workers.Add(worker);

			if (ID == 5)
				creatingThreads = false;
		}

		public static void OnThreadWorkerCompleted(object sender, ThreadWorkerCompletedEventArgs e)
		{
			if (sender is Worker)
			{
				Console.WriteLine(string.Format("Removing worker with ID - {0}", (sender as Worker).ID));
				workers.Remove(sender as Worker);
				if (!creatingThreads && workers.Count == 0)
					reset.Set();
			}
		}
	}

	public delegate void ThreadWorkerCompletedEventHandler(object sender, ThreadWorkerCompletedEventArgs e);

	public class ThreadWorkerCompletedEventArgs : EventArgs
	{
		// Ideally you really want to use items that can identify the thread.
		public int ID { get; private set; }

		private ThreadWorkerCompletedEventArgs()
		{
			ID = default(int);
		}

		public ThreadWorkerCompletedEventArgs(int ID)
		{
			this.ID = ID;
		}
	}
}

Open in new window


-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
Thanks for the example.

I have adjusted it for my code and it works great. Hopefully you will get these points.
0
it_saigeDeveloperCommented:
It was my pleasure.

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.