0

Is it possible to repeat multiple times the ManualResetEvent?

Something like this:

receivedDone.WaitOne();
//something here
receivedDone.Set(); //this go back to receivedDone.WaitOne()
//when executing the second time will loop the receivedDone.Set() and not returning 
//again to receivedDone.WaitOne(); like I wanted.

So my question is:

Is it possible to execute multiple times like a loop the same WaitOne(); and Set();?

EDIT:

I have a button, when I click it run a function to start my tcpclient.

After that I wait for some response from the server with the receivedDone.WaitOne(); when I receive the message on my buffer, it goes to receivedDone.Set(); . This works 1 time, but I want to make it multiple times with the same WaitOne(); and Set();

Is this possible?

Mikev
  • 2,012
  • 1
  • 15
  • 27
  • Could you show a little bit more code. Where is the loop which will repeat the call of WaitOne and set multiple times. – H.G. Sandhagen May 16 '19 at 17:15
  • That's what I wan't. I'll edit my question to explain better my problem. – Mikev May 17 '19 at 08:16
  • This could only make sense if you put the WaitOne() call in the button's Click event handler. After which you'd Reset() so it is usable again. But you're pretty unlikely to keep it, hanging the UI when the server is unresponsive is not a great idea. – Hans Passant May 17 '19 at 10:27

1 Answers1

2

As the name says, a ManualResetEvent must be reset manually. It is like a door. It is initialized with

ManualResetEvent ev = new ManualResetEvent(false);  // The door is closed

or

ManualResetEvent ev = new ManualResetEvent(true);  // The door is open

A thread, which calls WaitOne passes the door if is is open, otherwise waits at the door until it opens.

A call of

ev.Set();

opens the door and a call of

ev.Reset();

closes the door.

As far as I understand your question, an AutoResetEvent would help more. Or even better create a async function which proceed the TCP call and returns the result.

H.G. Sandhagen
  • 772
  • 6
  • 13