Aldaviva / AuthenticatorChooser

🗝️ Background program that skips the phone option and chooses the USB security key in Windows credential prompts.
Apache License 2.0
39 stars 0 forks source link

Allow only single instance #7

Closed waltersenekal closed 4 hours ago

waltersenekal commented 1 day ago

Hi, thanks for sharing your program, i love it I would suggest that you make it that only one instance can run at a time

internal static class Program
{

  static Mutex mutex = new Mutex(true, "{B13D5A64-7F53-48A8-B022-3F65E35C1A1E}");

  [STAThread]
  public static void Main()
  {
    if (mutex.WaitOne(TimeSpan.Zero, true))
    {
      Application.SetCompatibleTextRenderingDefault(false);
      Application.EnableVisualStyles();
      Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

      using WindowOpeningListener windowOpeningListener = new WindowOpeningListenerImpl();
      windowOpeningListener.windowOpened += (_, window) => SecurityKeyChooser.chooseUsbSecurityKey(window);

      foreach (SystemWindow fidoPromptWindow in SystemWindow.FilterToplevelWindows(SecurityKeyChooser.isFidoPromptWindow))
      {
        SecurityKeyChooser.chooseUsbSecurityKey(fidoPromptWindow);
      }

      _ = I18N.getStrings(I18N.Key.SMARTPHONE); // ensure localization is loaded eagerly

      Application.Run();
    }
    else
    {
      // Notify the user and exit
      MessageBox.Show("Another instance of the application is already running.");
    }
  }

}
Aldaviva commented 1 day ago

Thanks for the suggestion, that's a great idea. I will add that.

Aldaviva commented 4 hours ago

Added, this program will now exit if there is another instance of itself already running in the same user account.

Aldaviva commented 46 minutes ago

Released in 0.2.0.