help with multitherads in C#

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

Hi,

I have a server which span two threads and I want to be able to signal the
threads to stop
in order that my server will shut down properly

in the old days of winapi I did it by using a named event created by the
server,
a call to WaitForSingleEvent in each thread loop with the minimum time out
possible
(if the event was signaled the thread should exit the loop)

and a wait on the threads handle to detect when they did stop

how can I achieve the same in C#?

thanks in advance
 
Ummm, with an event?

They are not named b ut you can still have it visible from a shared member variable or a static member of a type they both know about. Also booleans work fine as flags too

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I have a server which span two threads and I want to be able to signal the
threads to stop
in order that my server will shut down properly

in the old days of winapi I did it by using a named event created by the
server,
a call to WaitForSingleEvent in each thread loop with the minimum time out
possible
(if the event was signaled the thread should exit the loop)

and a wait on the threads handle to detect when they did stop

how can I achieve the same in C#?

thanks in advance
 
Hi,

IIRC there is really no a clean method to signal a thread to ends, you can
declare the threads as worker ( Thread.IsBackground=true ) this will prevent
that the process be kept alive when the "main" thread is ended.

take a look in the archives for further discussion about this, it has been
discussed before.
 
In .Net framework, the equivalent classes for a similar event are
ManualResetEvent and AutoResetEvent. They are both derived from
WaitHandle (an abstract class). WaitHandle has three methods, WaitOne,
WaitAll and WaitAny, which will allow you do the waiting as you were
doing it before.
 
Ajay Kalra,

When I use CreateEvent(), I supply a string for the title of the Event. How
do I set the name of the event using AutoResetEvent? I appreciate any help.

Nick Nezis
 
Back
Top