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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

But my MainWindow class(the class where the application is running from) cannot inherit from Application.
Ô_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.
Do I have a way to exit the application from the OnStartApp method? the application continues to the MainWindow() constructror.
According to this yes. Google is your friend ;o)
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.
|
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.