1

I have a method which is running very long. I want to be able to stop, pause and continue the execution of that method.I viewed this question and the answer is nice, but I want to use more general library like TPL. TPL has a cancellation mechanism, but I can't find pause/continue functionality.

edit

Thanks for all answers. But the problem is that I don't write that "long running" method, I just call that method.

Community
  • 1
  • 1
user179437
  • 713
  • 1
  • 7
  • 16

3 Answers3

2

The easiest way to implement this is with a BackgroundWorker control. It supports cancel and progress reporting.

http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/48305e65-cea6-4a88-8818-c122e152daa8

EDIT

Oh, I see. Well, there is no "clean" way of pausing and resuming a method that you didn't write, unless you want to suspend the thread. And, if the method isn't thread-safe, you might be out of luck. http://msdn.microsoft.com/en-us/library/d00bd51t.aspx

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • Is it possible to pause/resume the BackgroundWorker ? – user179437 Mar 31 '11 at 21:24
  • Yes, check out the second link. Essentially, you add a `Paused` property to the BackgroundWorker (by creating a new class and inheriting from it). Then, in your DoWork event, you can add manual `while (Paused) Thread.Sleep(10);` calls wherever pausing is possible. – Chris Laplante Mar 31 '11 at 21:26
  • @SimpleCoder: I think you got your wiki markup and markdown mixed up. Could you edit your post to be more readable? – sehe Mar 31 '11 at 21:36
  • You really don't need sleep see [this](http://stackoverflow.com/questions/1096794/is-sleep-evil) – Conrad Frix Mar 31 '11 at 21:37
  • @sehe: I'm sorry, but I don't see what you mean. What part is messed up? – Chris Laplante Mar 31 '11 at 21:37
  • well hard to show you. the links are on a single line in Opera browser. Nevermind, chrome looks better already – sehe Mar 31 '11 at 21:39
  • @sehe: Oh, I've add a line between the links anyway – Chris Laplante Mar 31 '11 at 21:40
  • @Conrad Frix: Falaina's answer doesn't really address **why** it is impractical. According to C# in a Nutshell (latest edition), in response to this: `while (!proceed);`: "In general, this is very wasteful on processor time: as far as the CLR and operating system are concerned, the thread is performing an important calculation, and so gets allocated resources accordingly! Sometimes a hybrid between blocking and spinning is used: `while (!proceed) Thread.Sleep (10);` Although inelegant, this is (in general) far more efficient than outright spinning." – Chris Laplante Mar 31 '11 at 21:43
  • @SimpleCode Ok shouldn't have linked to a language-agnostic answer. Here'a C# [answer](http://stackoverflow.com/questions/5424667/alternatives-to-thread-sleep/5424747#5424747) from Jon Skeet. – Conrad Frix Mar 31 '11 at 21:51
  • @Conrad Frix: I agree, but that answer doesn't correspond to the original stance you took. Of course spinning isn't as efficient as a Monitor, but I never said it wasn't. – Chris Laplante Mar 31 '11 at 21:55
  • @SimpleCode My only stance is that you don't need to use sleep and in the case you probably shouldn't.If you re-read the page you'll notice that Mr. Albarhari cautions "Spinning very briefly can be effective when you expect a condition to be satisfied soon (perhaps within a few microseconds)" In this case we don't have a reasonable expectation that the user will hit resume within a few microseconds. – Conrad Frix Mar 31 '11 at 22:17
1

I think you've already answered your own question. If you need pause/continue functionality you need to set up some protocol. As the answer to the question you referenced using reset events is the way to go.

If you only need to do Cancel as you said the Task Parallel Library (TPL) has it with the CancelationToken.

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
0

If it is very long and needs to be persistent you might want to have a look at Windows Workflow Foundation.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Thanks for answer, but it is not that kind of "long running", it is just do very long calculation. – user179437 Apr 08 '11 at 07:03
  • The pausing functionality triggered me to give this option. When pausing for a long time (who knows how long...) keeping the state of the process can be pretty hard. A simple workflow can do this for you. – Emond Apr 08 '11 at 07:08