0

I've been wondering is there any way in which we can move BackgroundWorker to sleep and resume it again just like thread. I've searched in many forums in vain. None of them show any method which would do that. I checked Microsoft documentation and found out there isn't any predefined methods.

I know the workarounds by using resetEvents. Just asking for any other possible and much easier way.

Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • BackgroundWorker is obsolete. Anything you can do with it can be done in a better way with the Task Parallel Library. As @SomeUser answered, you can pause a task. – Panagiotis Kanavos Sep 30 '16 at 13:35

2 Answers2

3

If you use Task instead of BackgroundWorker you can use the PauseTokenSource.

This class is similar to the built in CancellationTokenSource only suitable for pausing tasks and not canceling them.

PauseTokenSource API was built exactly for what you need and it's API can replace your usage of Thread.Sleep and all the signaling events.

Other option besides PauseTokenSource can use AsyncManualResetEvent, the mechanism internal is quite similar but they differ in the API. I think that PauseTokenSource is much more convenient and especially built for this purpose, more info here.

Community
  • 1
  • 1
YuvShap
  • 3,825
  • 2
  • 10
  • 24
  • It Works but implementation of BackgroundWorker is more easier and useful when you need to report progress back to UI. With Task, it would be cumbersome. – Prajwal Sep 30 '16 at 13:35
  • You can report progress back to UI using Task in a really simple way http://blog.stephencleary.com/2012/02/reporting-progress-from-async-tasks.html – YuvShap Sep 30 '16 at 13:38
  • Task is kind of the next generation of the BackgroundWorker http://stackoverflow.com/questions/3513432/task-parallel-library-replacement-for-backgroundworker – YuvShap Sep 30 '16 at 13:39
  • I never explored Task before. It is pretty useful. Thanks. – Prajwal Sep 30 '16 at 13:40
0

From within your DoWork handler, you can call Thread.Sleep() whenever you want. If you want, from the GUI, to be able to signal the worker to pause, set up a concurrent queue, feed your sleep requests into it from the GUI thread, and have your DoWork handler check the queue periodically, pausing as requested.

(If you want to pause the BackgroundWorker until signaled again rather than for a certain period of time, you can do that in a similar way--just periodically check the queue for a "restart" command and sleep a few milliseconds before checking again.)

adv12
  • 8,443
  • 2
  • 24
  • 48
  • I can also add time limit for sleep and after certain time, I can cancel the thread. That would work, won't it? – Prajwal Sep 30 '16 at 13:29
  • I'm not totally sure I understand you. Yes, it would be possible for you to pass the sleep timeout via the queue. If you want to cancel the `BackgroundWorker` from the GUI thread, use the built-in [CancelAsync method](https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync(v=vs.110).aspx), and then check the worker should check periodically whether `CancellationPending` is `true`. – adv12 Sep 30 '16 at 13:33
  • Definitely give @SomeUser's answer a look. I think they propose a better solution, if you can use `Task`. – adv12 Sep 30 '16 at 13:34