Killing threads after closing the GUI

I need to complete a time-consuming task on two to four different servers.

The number of servers may vary.

I need to prepare the same task on each machine at about the same time, otherwise it will quadruple the time it takes for this process.

So far I have managed to do this:

public partial class Form_main : Form
{
    list<string> vms = new list<string>;
    ///Fill in vms...
    private void startTest()
    {
        ///prevents GUI from hanging
        ThreadPool.QueueUserWorkItem(new WaitCallback(runTests));
    }
    pirvate void runTests()
    {
        ///Some setup stuff (~1 min)
        foreach(string vm in vms)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(testProcedure), new object[] {vm});
        }
    }
}

This works to launch everything at once, but the problem is that if the GUI is killed, threads will not be killed along with it. I assume that it kills the runTests thread, but does not kill the testProcedures threads.

How can I make testProcedures threads kill when the GUI closes?

+4
source share

All Articles