3

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?

leko
  • 2,308
  • 1
  • 11
  • 11
  • 1
    Maybe [this](http://stackoverflow.com/questions/3793997/pass-arguments-to-running-application) will help. – Bali C Jan 10 '13 at 12:27
  • 1
    We use WCF (mentioned in some of the links already posted, so I won't relink). WCF is fiddly to use at first, but it is (IMHO) the best way to do this kind of interprocess communications. – Matthew Watson Jan 10 '13 at 12:31
  • 2
    Check this link http://stackoverflow.com/questions/4541354/simple-communication-between-2-instances-of-application – Prasanth V J Jan 10 '13 at 12:37
  • Thanks for all the great comments and answers, been of great services. Much appreciated! – leko Jan 10 '13 at 12:42

2 Answers2

4

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

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

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!

leko
  • 2,308
  • 1
  • 11
  • 11