Is there a way to wait for a ManualResetEventSlim to be Reset()?

David Thielen 3,211 Reputation points
2023-11-30T22:05:28.58+00:00

Hi all;

The call ManualResetEventSlim.Wait() waits for the event to be set. I need the opposite, it has been set and I'm now waiting for it to be reset. Is there a way to do that?

I can't flip my event object, it is used to process a queue of messages writing them to a BLOB. But when the app is exiting, I need call Set(), the queue processing runs and writes everything, calls Reset(), and then my shutdown code can continue.

thanks - dave

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. David Thielen 3,211 Reputation points
    2023-12-01T16:58:59.67+00:00

    I did something similiar. I hate polling but in this case, probably better than a second ResetEvent and the potential race conditions between them.

    			_newItemEventSlim.Set();
    			// wait for _newItemEventSlim to be reset
    			for (var delayMilliseconds = 0; delayMilliseconds < 500; delayMilliseconds *= 2)
    			{
    				if (!_newItemEventSlim.IsSet)
    					break;
    				Thread.Sleep(delayMilliseconds);
    			}
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.