0
public partial class App : System.Windows.Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var args = e.Args;
        if(args.Length!=0)
        {
            if (args[0] == ("login"))
            {
                new LoginWindow().Show();//conduct the login procedure
            }
            if (args[0] == ("restart"))
            {
                new SmallPanelWindow(2).Show();
            }
        }
        else
        {
            Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--proxy-server=\"direct://\"");
            new MainWindow().Show();//show mainwindow which is commonly used
        }
    }

How can I ensure the LoginWindow exist only one at a time when the program is called with parameter "login" for multiple times?(eg. different triggers in windows TaskScheduler call the program with "login")Also, Starting Mainwindow can't be affected.

1 Answers1

1

You could use a Mutex to ensure that only a single process at a time can open the LoginWindow:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    var args = e.Args;
    if (args.Length!=0)
    {
        if (args[0] == ("login"))
        {
            using (Mutex mutex = new Mutex(false, "8da112cb-a833-4c50-be53-79f31b2135ca"))
            {
                if (mutex.WaitOne(0, false))
                {
                    new LoginWindow().ShowDialog(); //conduct the login procedure
                }
                else
                {
                    Environment.Exit(0);
                }
            }
        }
        if (args[0] == ("restart"))
        {
            new SmallPanelWindow(2).Show();
        }
    }
    else
    {
        Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--proxy-server=\"direct://\"");
        new MainWindow().Show();//show mainwindow which is commonly used
    }
}

You may want to to move the initialization of the SmallPanelWindow inside the using statement depending on your requirements but the above sample code should give you the idea.

mm8
  • 163,881
  • 10
  • 57
  • 88