-1

Can this solution work? Is there a way to indefinitely pause a thread

Is the resources released with this solution?

Community
  • 1
  • 1
Prince OfThief
  • 6,323
  • 14
  • 40
  • 53

2 Answers2

2

In short, no. The only way to stop what a thread is doing and release any resources being used is to Abort it (Thread.Abort method).

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • (I prefer to have the thread "terminate" itself. Then it can perform tear-down cleanly itself.) –  Jan 13 '11 at 03:55
  • 1
    Agreed- I was presupposing that this had to be an externally-imposed shutdown, and that the process running in the thread wasn't always checking whether it should be shutting down. – Chris Shain Jan 13 '11 at 04:00
0

Unless there is a lot more to this question, just end the thread at an "indefinite" pause and create a new thread after the pause. When the thread isn't running just release all the restorable data/resources that make sense (from within the thread itself, if possible). Take time to come up with a clear life-cycle and resource-ownership model. In general, let the thread be "cooperative" and manage its own resource lifetime.

The general create/start/thaw/work/freeze/stop phases is the underlying basis of many long-running and/or pause-able jobs such as SharePoint Timers, Windows Workflow Tasks or SQL Server agents. Note that there is no "suspended indefinitely" phase in the list above -- this is just the space between the create and the start (or the stop and the next start) and is normally where external events enter the system.