I am very new to threading, as a beginner in C#. I have a program that will be firing multiple threads inside of a windows app. My aim is to start a new thread for every item within a list. The items in this list are workstation names on anetwork. Each thread that is created will look to do repairs on each machine, when the thread has finished it will write to a log file of any errors found etc. But what i want to be able to determine is when all threads have finished. So if i have 100 machines, 100 threads, how do i determine when all have closed?
Heres my method below :-
private void repairClientsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (machineList.Count() != 0)
{
foreach (string ws in machineList)
{
new Thread(new ParameterizedThreadStart(fixClient), stack).Start(ws);
}
}
else
{
MessageBox.Show("Please import data before attempting this procedure");
}
}
The way to do this would be to keep a reference to all the threads and then Join on them. This basically means that the current thread will block until the joined thread completes.
Change your loop to something like:
(where _machineThreads is a list of
System.Thread)You can then block until all are complete with something like:
HOWEVER – you almost certainly don’t want to be doing this for the scenario you describe: