简体   繁体   中英

C# background worker

I have a task running in backgroundworker. on clicking the start button user starts the process and have got one cancel button to cancel the processing.

When user clicks on cancel, I would like to show a message box that "Process has not been completed , do you want to continue".

Here I want the processing which is left to be done only after user input. Till then want to halt the back ground thread. Can anyone help me on this. Is there anyway to halt the background worker for some time .Any kind of help will be appreciated.

Not built in. You could tell your code to (on every [n] loop iterations, etc) check something like a ManualResetEvent to see if it should keep running (at the same time it checks for cancellation). I don't recommend suspending the thread ( Thread.Suspend ), since you don't know what locks etc it may hold at the time.

On the other hand... why not let it run until you know it should be cancelled? Then you just need to check for cancellation (there is a flag for this) every [n] iterations...

If the BackgroundWorker is working on an object that is visible to both threads, you could 'lock' that object while waiting for the user to answer the question in a dialog box. This will cause the worker's thread to halt until the dialog-generating thread ends the lock.

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