I am trying to cancel multiple threads at the click of a button in WinForms. My threading method is listed below, which the thread then calls another method. I created the thread as a class variable and then loops through and creates a new one for however many threads the user wants. I want to stop call threads at the click of a stop button. I could not find any similar questions. Originally, in my generate()
method below, I would check if the button was clicked and would Abort()
, but this only works if the button is clicked then and there.
private static Thread t;
private void threader()
{
int wait = (int)(waiter.Value * 60000);
for(int i = 0; i < numOfThread.Value; i++)
{
try
{
t = new Thread(new ThreadStart(generateBlizz));
t.IsBackground = true;
t.Start();
}catch(ThreadAbortException e)
{
}
}