public void ClickJobWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; while (true) { for (int i = 0; i < 2 * seconds; i++) { System.Threading.Thread.Sleep(500); //wait half a second and each time, check if cancel has been clicked if (ClickJobWorker.CancellationPending == true) { ClickJobWorker.CancelAsync(); e.Cancel = true; return; } } MouseClicker(); } }
private void StopButton_Click(object sender, EventArgs e) { ClickJobWorker.CancelAsync(); }