Skip to content

Commit

Permalink
Fix issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilkd committed Apr 30, 2022
1 parent aa19777 commit 2ec8acd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/SingleInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,33 @@ public static bool InitializeAsFirstInstance<T>(this T instance, string uniqueNa

private static void SignalFirstInstance(string channelName, IList<string> 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);
Expand Down

0 comments on commit 2ec8acd

Please sign in to comment.