Anees
Anees

Reputation: 873

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.

Upvotes: 7

Views: 709

Answers (2)

Assaf
Assaf

Reputation: 1346

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.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062502

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

Upvotes: 5

Related Questions