public void Run() { log.Info("starting relay host"); log.Debug("reset stop event and clear ports exceptions"); stop.Reset(); portsExceptions = new List <Exception>(); log.Debug("start asynchronous reading port A"); portA.BeginRead(delegate(IAsyncResult _ar) { ((Relay)_ar.AsyncState).ReadPortACallback(_ar); }, this); log.Debug("start asynchronous reading port B"); portB.BeginRead(delegate(IAsyncResult _ar) { ((Relay)_ar.AsyncState).ReadPortBCallback(_ar); }, this); log.Debug("wait for stop event"); stop.WaitOne(); log.Debug("stop event"); log.Debug("check exceptions"); if (portsExceptions.Count > 0) { log.Debug("generate aggregated exception"); throw new AggregateException(portsExceptions); } log.Info("relay host stopped"); }
public Scan Sync(int timeout = Timeout.Infinite) { log.Debug("synchronize processes"); log.Debug("clear stop and sync events and clear port exception"); stop.Reset(); sync.Reset(); portException = null; log.Debug("begin asynchronous read port"); port.BeginRead(delegate(IAsyncResult _ar) { ((Scan)_ar.AsyncState).SyncReadCallback(_ar); }, this); log.DebugFormat("wait for stop event (timeout={0})", timeout); if (!stop.WaitOne(timeout)) { throw new TimeoutException("Processes synchronization timeout."); } log.Debug("stop event"); log.Debug("check ports exceptions"); if (portException != null) { log.Debug("re-throw port exception"); throw portException; } log.Debug("check sync event"); if (!sync.WaitOne(0)) { throw new SynchronizationException(); } log.Debug("processes synchronized"); return(this); }
private void SyncWriteCallback(IAsyncResult ar) { try { log.Debug("end asynchronous write port B"); portB.EndWrite(ar); log.Debug("begin asynchronous read port B"); portB.BeginRead(delegate(IAsyncResult _ar) { ((Relay)_ar.AsyncState).SyncReadCallback(_ar); }, this); } catch (Exception ex) { log.Error("unhandled exception port B", ex); portsExceptions.Add(ex); stop.Set(); } }