From 2ec8acd18223fde24a6d3da12244bd5849435dce Mon Sep 17 00:00:00 2001 From: soheilkd Date: Sat, 30 Apr 2022 14:10:16 +0430 Subject: [PATCH] Fix issue #9 --- src/SingleInstance.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/SingleInstance.cs b/src/SingleInstance.cs index 9c970e2..f01baba 100644 --- a/src/SingleInstance.cs +++ b/src/SingleInstance.cs @@ -39,15 +39,33 @@ public static bool InitializeAsFirstInstance(this T instance, string uniqueNa private static void SignalFirstInstance(string channelName, IList commandLineArgs) { - var minMessageAge = TimeSpan.FromSeconds(30); - var bus = new TinyMessageBus(channelName, minMessageAge); + var bus = GetTinyMessageBus(channelName); var serializedArgs = commandLineArgs.Serialize(); - bus.PublishAsync(serializedArgs).Wait(); + bus?.PublishAsync(serializedArgs).Wait(); WaitTillMessageGetsPublished(bus); } + private static TinyMessageBus GetTinyMessageBus(string channelName, int tryCount = 50) + { + int tries = 0; + var minMessageAge = TimeSpan.FromSeconds(30); + while (tries++ < tryCount) + { + try + { + var bus = new TinyMessageBus(channelName, minMessageAge); + return bus; + } + catch (Exception) { } + } + return default; + } + private static void WaitTillMessageGetsPublished(TinyMessageBus bus) { + if (bus == null) + return; + while (bus.MessagesPublished != 1) { Thread.Sleep(10);