John Bustos
John Bustos

Reputation: 19544

.Net Program with both WinForm and Console Interfaces

I have a simple question and I'm sure it's been answered, but I can't seem to find the solution I'm looking for.

My basic question is that I've created a console app in .Net that runs automatically on a task scheduler every day, but now my clients also want a windows form-based interface that they can use to run special runs (they use the GUI to specify a few parameters - such as specific dates, etc - to make the program run a bit differently).

The way I thought to do this would be to convert my console app to a WinForm solution and to include Command Line Arguments for when I wanted it to run as the original console app with defaults, but I'm thinking that's not the right way since it would still involve a form load.

My other thought was to convert the "engine" part to a DLL and to make 2 executables - One a console app and one a winforms app, but I'm not sure if that's right either / could lead to more maintenance.

What is the correct way to do this?

I'm writing this in VB, but am equally comfortable with C# solutions if that is easier for you.

Thanks!!

Upvotes: 5

Views: 4623

Answers (5)

Biggles
Biggles

Reputation: 1345

Three projects as Reed suggested is a correct approach, however if you really need 1 executable (which we for some reason really wanted in one case) you can do following:

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
      if (args[0]== "-ui")
      {
         System.Windows.Forms.Application.EnableVisualStyles();
         System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
         System.Windows.Forms.Application.Run(new MyFormWork());
      }
      else if (args[0] == "-console")
      {
         Helper.AllocConsole();
         DoYourConsoleWork();
         Helper.FreeConsole();
      }
}

    public static class Helper
    {

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }

Upvotes: 3

naqib
naqib

Reputation: 70

1: you can use XML for arguments. your console just need to read XML for its argument. then create a form app just for editing XML and save.

Benefit: your app is running by task scheduler so your form do not need to . for user it's easy to open form app and change something that will save to xml

Console --run every day without any notice
Argument.xml  -- argument for Console .
Form  -- user interface 

2: you can mix both within a form but it will run every day in form base not good idea

Upvotes: 0

Gusman
Gusman

Reputation: 15151

You can modify your Program.cs to accept arguments, then if some args had been passed to your app prcess them and exit from main, else start your main form, something like this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {

        if (ProcessCommandLine(args))
            return;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    static bool ProcessCommandLine(string[] args)
    {
         //Process it, if some has been processed return true, else return false
    }
}

Upvotes: 9

pposada
pposada

Reputation: 178

Or you can make the form invisible and go with your first option, reading the commandline.

here is a way of making the invitial form invisible. Form Invisible

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564413

Typically, I'd split the logic into a library, then make a simple console app and a simple Forms app to call into the logic in the library.

You can then distribute these separately and use them as they are intended, without duplication of code.

Upvotes: 12

Related Questions