From: Robe on
Hi there,

I'm doing my first managed threading application and I need to use the
Suspend and Resume methods from Thread class but the compiler issue a message
saying that these methods are obsoletes.

What alternative I can use? Because I need to suspend and resume my thread.

Thank you.
From: Peter Duniho on
Robe wrote:
> Hi there,
>
> I'm doing my first managed threading application and I need to use the
> Suspend and Resume methods from Thread class but the compiler issue a message
> saying that these methods are obsoletes.
>
> What alternative I can use? Because I need to suspend and resume my thread.

No, you don't. You only think you do.

The methods are obsolete because they are never a good way to manage
thread execution state.

The correct way to do so would be to use one of the many and various
thread synchronization mechanisms available. For example, the Monitor
class (used implicitly by the "lock" statement), the various WaitHandle
sub-classes (e.g. AutoResetEvent), or task-specific synchronization
classes (e.g. Mutex, ReaderWriterLockSlim, etc.).

Using these primitives, a thread will _block_ and _continue_ at
appropriate, well-defined places, but will not become suspended, nor
need resuming. While blocked, the thread will not consume any CPU
resources.

Without a more specific explanation regarding the exact algorithm you're
trying to implement, I can't say which synchronization mechanism is
preferable. But my general preference, absent any other information, is
to use Monitor for synchronization, and either AutoResetEvent or
ManualResetEvent (depending on needs) for simple "start/stop" logic.

Pete
 | 
Pages: 1
Prev: RDA (remote data access)
Next: Searching XML