Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle Command Line Arguments in Winforms if I don't want to load Main form?

I want to create an app that behaves as follows:

  1. On no argument it displays the main form
  2. On argument "a" does a job but the main form isn't loaded.
  3. On argument "b" the form loads using the argument passed (load that document)

For the 1 and 3 I can handle the arguments in the form's constructor as follows:

public ConfigurationActionManagerForm()
{
    InitializeComponent();
    Environment.GetCommandLineArgs();
    // do stuff with that argument
}

But this approach doesn't allow me to apply the behavior of 2. in the list.

In program.cs I can edit it to handle the arguments before the form is even created, but what is the correct approach on using Application.Run() if I don't want to pass a form? How am I going to inform Program class instance that I need to terminate or show a message that something went wrong or even show a little taskbar icon that the process is doing stuff (Think of it like the unzipping process).

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new ConfigurationActionManagerForm());
}

Would this approach from MSDN be correct to my application?

like image 975
Odys Avatar asked Oct 26 '11 11:10

Odys


2 Answers

Do you mean in the same way that Visual Studio works?

If so then you can't do this in a normal Windows application - Visual Studio cheats.

The problem is that a Windows application can either be a Windows Forms application or a Console application, but it can't be both - its decided at compile time (for .Net applications this is in the project properties window). Your options are:

Make your application a Windows Forms application

In this case #1 and #3 will work perfecty, but for #2 you will find that you can't read from / write to the console (because there isn't one!). If your appliction doesn't need to give any feedback then this might be fine - do your work as you normally would and just don't display a form:

[STAThread]
static void Main(string[] args)
{
    if (args.Length > 0)
    {
        // Handle #2 here
    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ConfigurationActionManagerForm());
    }
}

Make your application a console application

In this case #2 will work perfectly, however although #1 and #3 will work fine you will always have console window open in the background - if you close the console window your application will end.

Again this might be fine, but personally I find this to be a hack.

Cheat (do what Visual Studio Does)

Visual Studio cheats by having 2 separate applications - one is a Console Application and the other is a Windows Forms application. The easy solution is to leave it at that and require that users start a different executable when running the command line version (e.g. myprogram_g.exe and myprogram_w.exe).

Visual Studio goes one step further however and has a single entry point, devenv. It does this by using the fact that for compatability reasons the Windows shell will always run a .com file instead of a .exe if there is any ambiguity. Wheras all shortcuts etc.. point to the executable, if you run devenv on the command line the devenv.com application will run instead which uses magic to sort out whether or not it runs as a console or windows application.

My advice would be to create two different applications and leave it at that.

See How do I write a program that can be run either as a console or a GUI application? for more detail (make sure to read the comments which have additional useful suggestions).

Also see How to make an application as both GUI and Console application? for how ildasm does this.

like image 66
Justin Avatar answered Sep 22 '22 21:09

Justin


You can call Application.Run() without a form instance.

That way, it will start the message loop without opening a form.

You can call MessageBox.Show() before calling .Run(), too.

You can even create and open a form, and then call Run() without specifying an argument - it just means that closing the form doesn't automatically exit the application.

E.g.

        MessageBox.Show("Messaage!");

        Form1 f = new Form1();
        f.Show();

        Application.Run();

As stated above, this way of doing Run() means that closing the forms doesn't automatically close the application. You need to handle this in the form's Close event handler. (Application.Exit())

MSDN online can help you out with this - check the help entry for Application.Run().

like image 32
Edwin Groenendaal Avatar answered Sep 22 '22 21:09

Edwin Groenendaal