3

I have a WPF application which can get parameters via command line. my application cannot be duplicate (I made it single instance) but I would like to be able to add parameters a few times - and to add them into the runnig application without opening another window. fo now in my code I check if the application has another instance - and if so I throw an exception. I'm looking for a way that I would by able to run the following script a few times:

Myapp.exe -firstname first -lastname last

and in every time the one running application would add the inserted parameters to its list. How can I do it?

user1016179
  • 499
  • 3
  • 10
  • 23

3 Answers3

2

You can override the OnStartup method of your App class. Pls have a look here. You will find the command line argument in the parameter of the method. Pls have a look here.

public class App : Application
{
    protected override OnStartup(StartupEventArgs e)
    {
        var cmdLineArgs = e.Args;
        // your logic here
    }
    // ...
}

Since you will need some mechanism for interprocess communication, you probably should read this SO post. I think creating a WCF service is the best option.

Community
  • 1
  • 1
DHN
  • 4,597
  • 3
  • 28
  • 45
  • But my MainWindow class(the class where the application is running from) cannot inherit from Application. – user1016179 Mar 13 '13 at 13:44
  • Ô_o that's right, but the entry point of a WPF application is not the `MainWindow`. It's the `App` class, which can usually be found in _App.xaml_ and _App.xaml.cs_, where you could implement the override. The rest should be easy from there. – DHN Mar 13 '13 at 13:49
  • Do I have a way to exit the application from the OnStartApp method? the application continues to the MainWindow() constructror. – user1016179 Mar 13 '13 at 14:23
  • According to [this](http://stackoverflow.com/questions/606043/shutting-down-a-wpf-application-from-app-xaml-cs) yes. [Google is your friend](https://www.google.de/search?q=exit+wpf+application+onstartup) ;o) – DHN Mar 13 '13 at 14:29
  • When I use that - If I direct to my MainWindow xaml - another window is open. But I would like to add the details I get as parameters to the current running window. – user1016179 Mar 13 '13 at 14:55
  • Ok, I see. I hoped you have already dealt with interprocess communication. Pls have a look [here](http://stackoverflow.com/questions/84855/what-is-the-best-choice-for-net-inter-process-communication). Since this is not an easy topic, it's the best opportunity I can provide. I also updated my post. – DHN Mar 13 '13 at 15:30
  • Did you mean that you are getting 2 instances of your MainWindow? If so check your App.xaml and make sure it isn't starting an instance of MainWindow – failedprogramming Mar 14 '13 at 13:41
  • This answer answered my problem. I did not know how to access the parameters from a URI Protocol. – Robert Apr 03 '13 at 04:56
0

When Performing the check if the application is single or not - in a case there is an instance of the application (Duplicate call)- the application is supposed to send an error message to the user. In this case you should check if there are command line parameters, and if so - just send Close() and return commands without showing any error message. The application will get the parameters from the command line and do with them what it knows to do.

user1016179
  • 499
  • 3
  • 10
  • 23
0

you can use Application.Current.Shutdown() to stop your application. This can appended before Window shown if it is call in the OnStartup.

For reading arguments, you can use e.Args in OnStartup or Environment.GetCommandLineArgs() everywhere.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        // check if it is the first instance or not
        // do logic

        // get arguments
        var cmdLineArgs = e.Args;

        if (thisisnotthefirst)
        {
            // logic interprocess
            // do logic

            // exit this instance
            Application.Current.Shutdown();
            return;
        }

        base.OnStartup(e);
    }

    protected override void OnExit(ExitEventArgs e)
    {
        // may be some release needed for your single instance check

        base.OnExit(e);
    }
}

I don't know how you check the single instance, but i use Mutex for that :

    protected override void OnStartup(StartupEventArgs e)
    {
        Boolean createdNew;
        this.instanceMutex = new Mutex(true, "MySingleApplication", out createdNew);
        if (!createdNew)
        {
            this.instanceMutex = null;
            Application.Current.Shutdown();
            return;
        }

        base.OnStartup(e);
    }

    protected override void OnExit(ExitEventArgs e)
    {
        if (this.instanceMutex != null)
        {
            this.instanceMutex.ReleaseMutex();
        }

        base.OnExit(e);
    }

Hop that will help you.

Xaruth
  • 3,968
  • 3
  • 17
  • 24