Search code examples
c#manualresetevent

ManualResetEvent (WaitOne and Set) repeating multiple times


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?


Solution

  • 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.