I have the following code and the thread is not stoping even if i close the form or exit the program with System.Windows.Forms.Application.Exit();
.
My code:
bool shouldStop = false;
private void button1_Click(object sender, EventArgs e)
{
backgroundThread = new Thread(
new ThreadStart(() =>
{
for (int j = 0; j <= 1000; j++)
{
if (!shouldStop)
{
//do something
}
else
{
break;
}
}
}));
backgroundThread.Start();
}
private void FormUpdateDB_FormClosing(object sender, FormClosingEventArgs e)
{
shouldStop = true;
}