sama
sama

Reputation:

How do I abort, pause and resume threads in C#?

How do I abort, pause and resume threads in C#?

Related question:

Is there a way to indefinitely pause a thread?

Upvotes: 9

Views: 14442

Answers (4)

Gordon Childs
Gordon Childs

Reputation: 36169

If you need to pause a thread for a while, get it to call Sleep.

If you need it to pause until some condition becomes true, use a condition variable or a wait.

For condition variables in .NET use Monitor methods including Wait and Pulse; see here for an introduction. (Windows Vista also added native Condition Variable support to the Win32 API, but that is a completely separate implementation, .NET has had conditional variable support since V1.0).

For Waits: that is ant use of WaitAny or WaitAny method overloads of the WaitHandle class to wait on one of the various Win32 synchronisation object types exposed in .NET as subclasses of WaitHandle (events, mutexes, waitable timers and so forth).

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063884

In general, don't pause (or abort) a Thread - you can't normally tell what it was doing, and this could lead to all sorts of locking issues, such as an a lock that isn't released, or a type initializer (static constructor) that is left hanging.

You might, however, pause code through more elegant methods - for example, using a ManualResetEvent in your loop that external code (on another thread) can open and close:

// worker loop
while(alive) {
    // if the gate is closed, wait up to 5 seconds; if still not
    // open, go back to the loop-start to re-check "alive"
    if (!gate.WaitOne(5000)) continue;

    // do work...
}

Then another thread with access (probably indirect) can pause (Reset) and resume (Set) the worker, but in the knowledge that it only pauses in a safe state.


Re your comment (on another reply) - it sounds like you have a reader and writer; ideal for a producer/consumer scenario. I have a an example on this question of how to write a producer/consumer with a capped size (so it won't swamp if the consumer runs slow) - the consumer blocks if there is no data, and the producer blocks if there is too much.

Upvotes: 23

Marcel Popescu
Marcel Popescu

Reputation: 3351

If you want to do it even though it's a bad idea, look at the Suspend and Resume methods - MSDN has more info on the subject, including why it's a bad idea.

To repeat - I strongly advise you to go with Marc Gravell's solution instead.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503140

If you've aborted the thread, it won't be able to run again. You could run the same task (e.g. the same ThreadStart delegate) on a different thread, but that's not running the same thread again.

I would strongly advise you not to hard-abort threads unless your app is coming down. Instead, work out a graceful thread termination protocol instead, so you can tell a task that you want it to stop. The same goes for "pausing" a thread - you really don't want to suspend a thread while it's holding some critical resource.

Perhaps if you could tell us more about your situation, we could help more. Graceful pause/resume or cancel functionality is reasonably easy to achieve with monitors (see the second half of the page) or wait handles.

Upvotes: 8

Related Questions