how to Block a method until another method executes in c# .net -
how to Block a method until another method executes in c# .net -
i have application, in i'm sending command hardware controller, controller responds command. in this, have queue of commands , send them 1 one, want send commands synchronously, means when receive first commands response send next command. have 2 methods, 1 send commands , handling received commands.
this called signaling , simplest way implement via manualresetevent
.
if phone call waitone
on manualresetevent
object, current thread gets blocked until thread "signals" go on calling set
on same object:
var signal = new manualresetevent(false); // instantiate in "unsignaled" state new thread (() => { console.writeline("sending command hardware controller..."); // send command // ... console.writeline("done."); signal.set(); // signal waiting thread can continue. }).start(); console.writeline("waiting hardware thread it's work..."); signal.waitone(); // block thread until signaled. signal.dispose(); console.writeline("got our signal! can continue.");
c# .net multithreading thread-synchronization
Comments
Post a Comment