.net - EventWaitHandle - multiple threads = inconsistent behaviour -


I am trying to set up some comics with a device where you can send commands and get a response Are there. However, I want to make sure that I control a timeout flag to prevent the waiting period of indefinite time.

I have done something like this:

  Private volatile eventwhathand_ignal; Send Public Zero () {// Some _signal.WaitOne (30000); // continue with _signal.Reset (); } Get public null () {_signal.Set (); }   

My question is, if I have many threads (let's say 2 for this example) that method can send and use the following scenario: < P> Thread A:

  // Sends a "listen" command, but send no response ();   

Thread B:

  // Sends a "canceled" order and response (success) is received Send ();   

I get inconsistent results, that is, sometimes both threads are released when I get a response to the second command (cancellation) which I can understand, but sometimes sometimes Thread kills the timeout of 30000 - which I can not understand.

Is it any advice I am missing and / or doing wrong?

An eventwavehold appropriate synchronization object is not here Here you only need a Thread can be in the situation where he sent the command and waiting for the response. In other words, the thread must take a turn to talk to the device strictly.

A manual reset event does not guarantee you, as soon as you set () then the all threads are blocking the WaitOne () call, they will be unblocked. If it is more than one, then your program will get spoiled when trying to send two or more threads at the same time.

A Mutex provides mutual exclusion. Similarly, its name was found.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -