Default to preventing multiple instances

Allow multiple instances if overridden with command line parameter
Allow starting minimised to tray with command line parameter
This commit is contained in:
Alexander Horner
2022-12-08 19:04:43 +00:00
parent 2a0d41759b
commit c4291e6f44
3 changed files with 58 additions and 13 deletions

View File

@@ -1,25 +1,46 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
namespace ntfysh_client
{
static class Program
{
private static readonly NotificationListener NotificationListener = new NotificationListener();
private static readonly NotificationListener NotificationListener = new();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
private static void Main(string[] args)
{
args = args.Select(a => a.ToLower()).ToArray();
if (args.Contains("-h") || args.Contains("--help"))
{
MessageBox.Show("Help:\n -h\n --help\n\nStart in tray:\n -t\n --start-in-tray\n\nAllow multiple instances:\n -m\n --allow-multiple-instances", "Help Menu", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
bool startInTray = args.Contains("-t") || args.Contains("--start-in-tray");
bool allowMultipleInstances = args.Contains("-m") || args.Contains("--allow-multiple-instances");
if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly()!.Location)).Length > 1)
{
if (!allowMultipleInstances)
{
MessageBox.Show("Another instance is already running.\n\nUse -m or --allow-multiple-instances if you wish to start a second duplicate instance.\n\nThis instance will now close.", "Multiple Instances", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(NotificationListener));
Application.Run(new MainForm(NotificationListener, startInTray));
}
}
}