Skip to content

Commit

Permalink
Merge pull request #1064 from Particular/session-mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanFeldman committed May 29, 2021
2 parents 1b2e183 + ad2b694 commit 2396398
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup .NET SDK
uses: actions/setup-dotnet@v1.7.2
with:
dotnet-version: 5.0.x
dotnet-version: 5.0.203 # should be 5.0.x when the runner image is updated to VS 16.10 or higher
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceInsight.Tests/ShellViewModelTests.cs
Expand Up @@ -21,10 +21,10 @@
using ServiceInsight.Framework.Licensing;
using ServiceInsight.Framework.Settings;
using ServiceInsight.Framework.UI.ScreenManager;
using ServiceInsight.Startup;
using Settings;
using Shell;
using Shouldly;
using Startup;

public interface IShellViewStub : IShellView
{
Expand Down
16 changes: 16 additions & 0 deletions src/ServiceInsight.Tests/Startup/PipeNameTests.cs
@@ -0,0 +1,16 @@
namespace ServiceInsight.Tests
{
using System;
using NUnit.Framework;
using ServiceInsight.Startup;

[TestFixture]
public class PipeNameTests
{
[Test]
public void Should_return_pipe_name_with_the_current_user_username()
{
Assert.AreEqual($"ServiceInsight-{Environment.UserName}", PipeName.Value);
}
}
}
7 changes: 4 additions & 3 deletions src/ServiceInsight/App.xaml.cs
Expand Up @@ -12,12 +12,13 @@

public partial class App
{
bool createNew;
Mutex mutex;
readonly bool createNew;
readonly Mutex mutex;

public App()
{
mutex = new Mutex(true, "ServiceInsight", out createNew);
mutex = new Mutex(true, "Local\\ServiceInsight", out createNew);

if (createNew)
{
LoggingConfig.SetupLogging();
Expand Down
8 changes: 4 additions & 4 deletions src/ServiceInsight/Startup/CommandLineArgParser.cs
@@ -1,9 +1,9 @@
namespace ServiceInsight.Startup
{
using System;
using System.IO.Pipes;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using Anotar.Serilog;
using ServiceInsight.Models;

Expand All @@ -13,8 +13,8 @@ public class CommandLineArgParser
const char TokenSeparator = '&';
const char KeyValueSeparator = '=';

EnvironmentWrapper environment;
IList<string> unsupportedKeys;
readonly EnvironmentWrapper environment;
readonly IList<string> unsupportedKeys;

public CommandLineOptions ParsedOptions { get; private set; }

Expand Down Expand Up @@ -44,7 +44,7 @@ public void SendToOtherInstance()

try
{
using (var pipe = new NamedPipeClientStream(".", "ServiceInsight", PipeDirection.Out))
using (var pipe = new NamedPipeClientStream(".", PipeName.Value, PipeDirection.Out))
using (var writer = new StreamWriter(pipe))
{
pipe.Connect(1000);
Expand Down
15 changes: 15 additions & 0 deletions src/ServiceInsight/Startup/PipeName.cs
@@ -0,0 +1,15 @@
namespace ServiceInsight.Startup
{
using System;

/// <summary>
/// NamedPipeServerStream pipe name.
/// </summary>
public static class PipeName
{
/// <summary>
/// "ServiceInsight-username" where username is the Windows currently logged in user username.
/// </summary>
public static string Value { get; } = $"ServiceInsight-{Environment.UserName}";
}
}
7 changes: 2 additions & 5 deletions src/ServiceInsight/Startup/StartupConfigListener.cs
@@ -1,15 +1,12 @@
namespace ServiceInsight.Startup
{
using System.IO.Pipes;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using Caliburn.Micro;
using ServiceInsight.Framework.Events;
using ServiceInsight.Framework.Settings;

public class StartupConfigListener
{
Expand Down Expand Up @@ -40,7 +37,7 @@ public static async Task Start(IEventAggregator eventAggregator, CommandLineArgP

static async Task Handle(IEventAggregator eventAggregator, CommandLineArgParser parser, CancellationToken cancellationToken)
{
using (var pipe = new NamedPipeServerStream("ServiceInsight", PipeDirection.In, 1, PipeTransmissionMode.Byte))
using (var pipe = new NamedPipeServerStream(PipeName.Value, PipeDirection.In, 1, PipeTransmissionMode.Byte))
using (cancellationToken.Register(() => Disconnect(pipe)))
{
await pipe.WaitForConnectionAsync(cancellationToken);
Expand Down

0 comments on commit 2396398

Please sign in to comment.