简体   繁体   中英

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.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM