Skip to content

Commit

Permalink
Disable launching multiple application instances
Browse files Browse the repository at this point in the history
  • Loading branch information
acidicMercury8 committed Jan 14, 2024
1 parent eeb78ae commit 45f1c46
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/ImeSense.Launchers.Belarus.Avalonia/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,38 @@
namespace ImeSense.Launchers.Belarus.Avalonia;

class Program {
private const string MutexName = "Belarus.Launcher.Avalonia";

private static Mutex? _mutex;

// 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.
[STAThread]
public static void Main(string[] args) {
#if DEBUG
StartApp(args);
#else
var isMutexCreated = false;
try {
StartApp(args);
} catch (Exception exception) {
Log.Error("{Message} \n {StackTrace}", exception.Message, exception.StackTrace);
throw;
_mutex = new Mutex(initiallyOwned: false, MutexName, out isMutexCreated);
} catch {
}
if (!isMutexCreated) {
return;
}

try {
#if DEBUG
StartApp(args);
#else
try {
StartApp(args);
} catch (Exception exception) {
Log.Error("{Message} \n {StackTrace}", exception.Message, exception.StackTrace);
throw;
}
#endif
} finally {
_mutex?.Dispose();
}
}

private static void StartApp(string[] args) {
Expand Down

0 comments on commit 45f1c46

Please sign in to comment.