Trevor Daniel
Trevor Daniel

Reputation: 3964

command line to make winforms run without UI

i am not really sure how to ask this question properly. so apologies in advance.

but it's basically around how to make an existing app, with a UI, run as a scheduled task with no UI.

background..

I have a winforms app written in vs2012 with 2 forms.

the first form is the login, straight forward, but currently expects user interaction of their username and password.

the second is the main form. which does the main work when you hit the "start" button.

what I am trying to achieve it is for me to send it some command line parameters that would run it without any ui as a scheduled task.

so, I'm guessing, I need get rid of needing the user to input login details. and somehow trigger the "start download" button and make it invisible.

I've worked out how to send command line parameters and can see how to get the software to do something different if it hears /silent but how do I hide the forms?

I'm lost.

any help would be much appreciated!

Upvotes: 4

Views: 6192

Answers (4)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

C# still has a Main() function. In a standard winforms solution, all it does is create your Form1 (or whatever it gets renamed to), and start the application event queue with that form.

For example, it should look something like:

public static void Main()
{
     Application.Run(new Form1());
}

Modify this function. If you see command line arguments, do whatever you have to. If there are none, let it do its normal magic with the form. When you're done, it would be something like:

public static void Main(string[] args)
{
    if (args.Length > 0) {
        // log in
        // do all the necessary stuff
    } else {
        Application.Run(new Form1());
    }
}

Upvotes: 4

Szymon
Szymon

Reputation: 43023

Modify the Main method which should be the entry point to your application. If there are any arguments, you don't need to instantiate and show any forms, just run the code to do your job without UI.

    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            // silent mode - you don't need to instantiate any forms
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

Upvotes: 3

evhen14
evhen14

Reputation: 1927

find static Main method in your solution. Inside that method you will have Application.Run(new Form()) or (form.Show() or ShowDialog()). So they key is to pass a parameter that will tell you now to call this method (Show method on forms)

The key is to have your business logic in a class that is independent of you form and use this class when you want to have GUI or when you want scheduled task

Upvotes: 2

Gary Walker
Gary Walker

Reputation: 9134

I answered this just other day -- on this question

Pay attention to the difference between code blocks -- the first block runs formless, the 2nd block is standard.

  if (ABCFile > 0)
  {
    var me = new MainForm(); // instantiate the form
    me.NoGui(ABCFile); // call the alternate entry point
    Environment.Exit(0);
  }
  else
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
  }

Upvotes: 2

Related Questions