AutoResetEvent/ManualResetEvent question

N

Nathan Diederich

Hi,

I'm currently writing a multithreaded app where one of the threads is
waiting on information from the others. This is not the main UI thread,
though the UI thread is the one that can signal this one to kill itself.

ThreadEntryPoint()
{
//setup
while (_continue)
{
_autoEvent.WaitOne();
//
//doing other stuff
//
}
//cleanup
}

My question is this...to kill this thread I have a method that changes
the bool _continue to false, but the while loop won't exit until
_autoEvent is signalled to release the thread. What would happen if I
have the method changing the bool also do _autoEvent.Close()?

- Would it hang because there is a WaitOne()?
- Would the WaitOne return true or false or throw an exception?

I'm hoping you can tell me that it returns a false so I can tell the
rest of the loop not to execute so I can exit the thread quickly.

Thanks,

Nathan
 
G

Guest

Hi Nathan,
calling Close on the ManualResetEvent will not cause any exception to be
thrown by the ManualResetEvent object. Just like you predicted, your code
will sit there and hang because Close will not signal the object, Close is
used to Dispose of the object.

What I would do is place a check after the WaitOne call to see if the rest
of the loop should be executed, such as:

while(_continue)
{
_autoEvent.WaitOne();

if(!_continue) break;

//if it gets here then we need to do the processing

}

Then in your close method change the boolean and signal the WaitOne event to
be set

public void StopLooping()
{
_continue = false;
_autoEvent.Set();
}


Hope that helps
Mark R Dawson
 
M

Metallikanz!

Both AutoResetEvent and ManualResetEvent types derive from the WaitHandle type. The Close method is defined as virtual in the base class and is not overridden in AutoResetEvent type. If you see the implementation in the base class using Reflector or something, you will see that it simply disposes the instance and suppresses finalization overhead. Use of the object instance after this method call would be unpredictable.

I am not exactly sure of what you want to do, why don't you use the overload of the WaitOne() method which allows you to pass a timespan object allowing you to implement a time-out kinda thing.

If this does not solve your problem, it would be nice if you can give a more specific description of what you exactly want. I am quite sure some threading expert of ours will be able to answer your query nicely.

HTH, Metallikanz!
 
N

Nathan Diederich

Mark,

I was pretty sure that was what I would have to do but I was not quite
sure on the behaviour of the event object.

Thank you

Nathan
 
N

Nathan Diederich

A previous reply gave me the information I needed, though your response
will be fuel for thought on a different part of the program.

Thanks
Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top