I'm having trouble canceling a background worker that has a Thread.Sleep(100)
in it.
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
int count;
try
{
count = int.Parse(textBox3.Text);
for (int i = 0; i < count; i++)
{
backgroundWorker1.ReportProgress((int)(((double)(i + 1) / count) * 1000));
//Computation code
Thread.Sleep(int.Parse(textBox4.Text));
}
}
catch (Exception ex)
{
request.DownloadData(url);
MessageBox.Show(ex.Message);
}
}
private void cancel_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
progressBar1.Value = 0;
}
If I remove the Thread.Sleep(100)
then the cancel works but otherwise it just keeps going (the progress bar doesn't stop).
EDIT: Added the rest of the code