Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing argument into backgroundWorker (for use as a Cancel button)
    primarykey
    data
    text
    <p>I'm new to C# and object-oriented programming in general. I've been trying to implement a "Cancel" button into my GUI so that the user can stop it mid-process.</p> <p>I read this question: <a href="https://stackoverflow.com/questions/7459647/how-to-implement-a-stop-cancel-button">How to implement a Stop/Cancel button?</a> and determined that a backgroundWorker should be a good option for me, but the example given doesn't explain how to hand arguments to the backgroundWorker.</p> <p>My problem is that I do not know how to pass an argument into backgroundWorker such that it will stop the process; I have only been able to get backgroundWorker to stop itself.</p> <p>I created the following code to try to learn this, where my form has two buttons (buttonStart and buttonStop) and a backgroundWorker (backgroundWorkerStopCheck):</p> <pre><code>using System; using System.ComponentModel; using System.Windows.Forms; using System.Threading; using System.Timers; namespace TestBackgroundWorker { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Set the background worker to allow the user to stop the process. backgroundWorkerStopCheck.WorkerSupportsCancellation = true; } private System.Timers.Timer myTimer; private void backgroundWorkerStopCheck_DoWork(object sender, DoWorkEventArgs e) { //If cancellation is pending, cancel work. if (backgroundWorkerStopCheck.CancellationPending) { e.Cancel = true; return; } } private void buttonStart_Click(object sender, EventArgs e) { // Notify the backgroundWorker that the process is starting. backgroundWorkerStopCheck.RunWorkerAsync(); LaunchCode(); } private void buttonStop_Click(object sender, EventArgs e) { // Tell the backgroundWorker to stop process. backgroundWorkerStopCheck.CancelAsync(); } private void LaunchCode() { buttonStart.Enabled = false; // Disable the start button to show that the process is ongoing. myTimer = new System.Timers.Timer(5000); // Waste five seconds. myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); myTimer.Enabled = true; // Start the timer. } void myTimer_Elapsed(object sender, ElapsedEventArgs e) { buttonStart.Enabled = true; // ReEnable the Start button to show that the process either finished or was cancelled. } } } </code></pre> <p>The code, if it worked properly, would just sit there for five seconds after the user clicked "Start" before re-enabling the Start button, or would quickly reactivate the Start button if the user clicked "Stop".</p> <p>There are two problems with this code that I am not sure how to handle:</p> <p>1) The "myTimer_Elapsed" method results in an InvalidOperationException when it attempts to enable the Start button, because the "cross-thread operation was not valid". How do I avoid cross-thread operations?</p> <p>2) Right now the backgroundWorker doesn't accomplish anything because I don't know how to feed arguments to it such that, when it is canceled, it will stop the timer.</p> <p>I'd appreciate any assistance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload