Question

This question might seem a bit vague but here we go.

I am preventing multiple instances of my wpf application, like so:

Process proc = Process.GetCurrentProcess();
if (Process.GetProcessesByName(proc.ProcessName).Length > 1)
{
    Application.Current.Shutdown();
    return;
}

if (e.Args != null && e.Args.Count() > 0)
{
    this.Properties["Magnet"] = e.Args;
}

Is it possible to pass the command-line arguments to the application already running?

Was it helpful?

Solution

No, it is not possible. You should use some of inter process communication techniques.

OTHER TIPS

For anyone interested in, I decided to use this code in my App.Xaml.cs:

[STAThread]
public static void Main()
{
    if (SingleInstance<App>.InitializeAsFirstInstance(UNIQUE))
    {
        var application = new App();
        application.InitializeComponent();
        application.Run();
        SingleInstance<App>.Cleanup();
    }
}

public bool SignalExternalCommandLineArgs(IList<string> args)
{
    // Use arguments
    return true;
}

UNIQUE is a constant string of 20 characters.

SingleInstance<App>.Cleanup() derives from ISingleInstanceApp which is defined in SingleInstance.cs and I also implemented ISingleInstanceApp in my application class.

Thanks for all the help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top