Zapnologica
Zapnologica

Reputation: 22556

How to stop / destroy a running operation in c#

I have a windows form with a start and cancel button.

The start button does the following:

bootLoader = new BootLoader(this, _form1);
bootloader.Start();

Now when the user clicks the start button, the BootLoader class writes data to the COM port.

What I want to know is that how can I stop this operation from completeing when the user clicks the cancel button.

Currently I just have the following:

this.Close();

But It just closes the form and the data carries on being written to the com port.

Upvotes: 0

Views: 127

Answers (1)

noelicus
noelicus

Reputation: 15055

Use a cancellable BackgroundWorker?

var bw = new BackgroundWorkder();
bw.WorkerSupportsCancellation = true;

Then in your DoWork worker function you'll need to monitor the CancellationPending flag which will become true after you call bw.CancelAsync().

Upvotes: 1

Related Questions