Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 455
Anees Avatar asked Jul 18 '10 10:07

Anees


2 Answers

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

like image 195
Marc Gravell Avatar answered Oct 05 '22 17:10

Marc Gravell


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.

like image 20
Assaf Avatar answered Oct 05 '22 19:10

Assaf