Search code examples
c#multithreadingmanualresetevent

How to wait for ManualResetEvent.WaitOne() to be reached before moving on?


I'm trying to use ManualResetEvent to make a semaphore-like situation and I have placed WaitOne, Set and Reset in their right places. The WaitOne instruction is called in a listener thread and is places right after a tcp reading:

var networkStream = _clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, Convert.ToInt32(_clientSocket.ReceiveBufferSize));
_mainthreadControl.WaitOne(Timeout.Infinite);

And Set and Reset instructions are called in another thread, so the socket won't get competed over:

try
{
    //some code
    _mainthreadControl.Reset();
    //some code that uses the same socket as above
    _mainthreadControl.Set();
}
catch (Exception ex)
{
    //ignored
}

But I need the code to stop when it reaches Reset, and only continue after the WaitOne is reached (and executed), so the code below Reset only runs after the competing thread is waiting.

I don't know if I was clear enough, so I'm glad to add details as needed. Thanks in advance.


Solution

  • If it suites for you. Please try to use additional AutoResetEvent. Like this:

    var _additionalControl = new AutoResetEvent(false);
    
    // code gap
    
    var networkStream = _clientSocket.GetStream();
    networkStream.Read(bytesFrom, 0, Convert.ToInt32(_clientSocket.ReceiveBufferSize));
    _additionalControl.Set();
    _mainthreadControl.WaitOne(Timeout.Infinite);
    
    // code gap
    
    try
    {
      //some code
      _mainthreadControl.Reset();
      _additionalControl.WaitOne(Timeout.Infinite);
       //some code that uses the same socket as above
    
      _mainthreadControl.Set();
    }
    catch (Exception ex)
    {
      //ignored
    }
    

    In turn I recommend to use System.Threading.Monitor class, cause it's more faster than ManualResetEvent(false), because it's restricted by the single process. If course if you don't need to use lock in another program.