Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/TwitchStreamingTools/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.Versioning;
using System.Threading;

using Avalonia;
using Avalonia.ReactiveUI;
Expand All @@ -13,14 +14,24 @@
namespace TwitchStreamingTools;

internal sealed class Program {
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.

/// <summary>
/// The name used to ensure only a single instance of the application is running.
/// </summary>
private const string MutexName = "TwitchStreamingTools_SingleInstance";

/// <summary>
/// The logger.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
/// <summary>
/// Used to prevent multiple instances of the application from running.
/// </summary>
private static Mutex? _mutex;

/// <summary>
/// Main entrypoint of the application.
Expand All @@ -31,7 +42,7 @@ public static void Main(string[] args) {
#if DEBUG
XmlConfigurator.Configure(new FileInfo("log4net.debug.config"));
#else
XmlConfigurator.Configure(new FileInfo("log4net.config"));
XmlConfigurator.Configure(new FileInfo("log4net.config"));
#endif

Log.Info("Started application");
Expand All @@ -40,11 +51,24 @@ public static void Main(string[] args) {
Log.Fatal("Unhandled exception", exceptArgs.ExceptionObject as Exception);
};

BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
// We only allow a single instance of the application to be launched at once (due to having only a single config
// file)
_mutex = new Mutex(true, MutexName, out bool onlyAppInstance);
if (!onlyAppInstance) {
Log.Info("Application instance already running. Exiting.");
return;
}

// Avalonia configuration, don't remove; also used by visual designer.
try {
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
finally {
// Release the mutex when the application exits
_mutex.ReleaseMutex();
_mutex.Dispose();
}
}

/// <summary>
/// Builds the avalonia application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>