c# - .NET 4.5 Semaphore WaitOne(0) doesn't block, but decrements semaphore count -


On top of .NET 4.5 if I use the WaitOne (Int32) method on some objects of the Sacchar class (or - usually -

WaitHandle class) As its logic with 0 (zero), it does not block and can be used to test the current status of the sequence:

"If millisecond timeout is zero , Then the method is not blocked. Wait and handle immediately. "

For me it is very deceptive (or Ramak too), because it does not block, but it decreases the marginal count of one, so if I use it like this:

  if (! MySemaphore.WaitOne (0 )) DoSomething ();  

So it can reduce my semaphore (if true), which is unable to run the other thread. So we can not use this to test the situation of Simraff. Should be the correct use

  if (! MySemaphore.WaitOne (0)) DoSomething (); Otherwise my face Release ();  

So it does not test the condition of Sikandra only! correct?

Correct, this not only tests the state of Sikandara, will be a better word

If millisecond timeout is zero, then the method is not blocked. The method attempts to take a waiting handle and immediately returns with the failure or failure of that effort.

Also your example is incorrect, the correct way to use it

  if (mySemaphore.WaitOne (0)) {try {doSomething (); } Finally {// only for call release when the wait-on return is correct, then in the end // it also in // block also ensure that it is always called mySemaphore.Release (); }} Else {// Some other reason because the resource was not available}  

You should only call mySemaphore.Release () , when mySemaphore .WaitOne returns will be correct, in your current example you are calling it only when it goes wrong.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -