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 StartButton_Click(object sender, EventArgs e) { string input = textBox1.Text; int n = int.MaxValue; if (int.TryParse(input, out n) && n > 0) { seconds = n; label2.Text = "Running"; label2.ForeColor = System.Drawing.Color.Green; StartButton.Enabled = false; ClickJobWorker.RunWorkerAsync(); } else { MessageBox.Show("Please enter a valid number bigger than 1."); } }
private void StopButton_Click(object sender, EventArgs e) { ClickJobWorker.CancelAsync(); }