/// <summary> /// Publishes a message on the bus, but on its own thread, with a 2 second timeout that will cancel the message /// </summary> public void Publish <T>(T msg) where T : Message { ThreadPool.QueueUserWorkItem(delegate { Exception ex = null; CancellationTokenSource cts = new CancellationTokenSource(); Task task = Task.Run(() => { try { using (cts.Token.Register(Thread.CurrentThread.Abort)) { bus.Publish(msg); } } catch (Exception e) { if (!(e is ThreadAbortException)) { ex = e; } } }, cts.Token); bool done = task.Wait(2000); if (ex != null) { throw ex; } if (!done) { cts.Cancel(); } }); }
/// <summary> /// Publishes a message on the bus, but on its own thread, with a 2 second timeout that will cancel the message /// </summary> public void Publish <T>(T msg, bool failureToast = false) where T : fastipc.Message.Message { ThreadPool.QueueUserWorkItem(delegate { Exception ex = null; CancellationTokenSource cts = new CancellationTokenSource(); Task task = Task.Run(() => { try { using (cts.Token.Register(Thread.CurrentThread.Abort)) { if (bus != null) { bus.Publish(msg); } } } catch (Exception e) { if (!(e is ThreadAbortException)) { ex = e; } } }, cts.Token); bool done = task.Wait(2000); if (ex != null) { throw ex; } if (done) { //if the service was down, get the current service status now... if (this._status == Statuses.CommunicationFailure) { UpdateStatus(Statuses.Listening); Publish(new GetStatus()); } } else { UpdateStatus(Statuses.CommunicationFailure); cts.Cancel(); if (failureToast) { SendToast("Failure", "The FingerPass Windows service did not respond. Is it stopped?"); } } }); }
static void Main(string[] args) { var pipeName = new SimpleStringPipeName( name: "Example", side: Side.Out); var bus = new NamedPipeBus(pipeName: pipeName); new ProcessBHost(bus); Console.WriteLine($"Process B Host Runing"); Console.WriteLine("type exit to close"); Console.WriteLine("type help for command list"); var exit = false; do { var cmd = Console.ReadLine(); if (string.IsNullOrWhiteSpace(cmd)) { continue; } switch (cmd.ToLower()) { case "exit": exit = true; break; case "ping": bus.Publish(new Ping()); break; case "help": case "?": Console.WriteLine("exit: exit program"); Console.WriteLine("ping: publish ping message"); Console.WriteLine("cls: Clear Screen"); break; case "cls": Console.Clear(); break; } } while (!exit); bus.Dispose(); }
private void can_exchange_messages() { var timeout = TimeSpan.FromSeconds(3); var processADone = new ManualResetEventSlim(false); var processBDone = new ManualResetEventSlim(false); // Process A Simulation new Thread(() => { var pipeName = new SimpleStringPipeName( name: "UnitTest", side: Side.Out); var bus = new NamedPipeBus(pipeName: pipeName); var inHost = new TestHandler( handleStringContent: msg => { _logger.WriteLine($"[ProcessA] Received new message of type {msg.GetType().Name}: {msg.Content}"); }, handleTestMessage: msg => { _logger.WriteLine($"[ProcessA] Received new message of type {msg.GetType().Name}"); processADone.Set(); } ); bus.Subscribe(inHost); bus.Publish(new StringContentMessage("Message from In")); bus.Publish(new TestMessage()); }) { IsBackground = true } .Start(); // Process B Simulation new Thread(() => { var pipeName = new SimpleStringPipeName(name: "UnitTest"); var bus = new NamedPipeBus(pipeName: pipeName); var outHost = new TestHandler( handleStringContent: msg => { _logger.WriteLine($"[ProcessB] Received new message of type {msg.GetType().Name}: {msg.Content}"); }, handleTestMessage: msg => { _logger.WriteLine($"[ProcessB] Received new message of type {msg.GetType().Name}"); processBDone.Set(); } ); bus.Subscribe(outHost); bus.Publish(new StringContentMessage("Message from Out")); bus.Publish(new TestMessage()); }) { IsBackground = true } .Start(); processBDone.Wait(timeout: timeout); processADone.Wait(timeout: timeout); Assert.True( condition: processBDone.IsSet, userMessage: "Failed to receive message from 'ProcessA'" ); Assert.True( condition: processADone.IsSet, userMessage: "Failed to receive message from 'ProcessB'" ); }