C# program with both Form and Console

Posted on 2009/04/29

4


If you need to code a C# program that has both a window interface and a console, follow these steps:

  • Create a new Windows Application project (called FormConsole, for example)
  • In the properties of the project, set Output Type as Console Application
  • Open the Program.cs file and paste the following code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace FormConsole
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 tf = new Form1();
            Thread ct = new Thread(
                new ThreadStart(
                delegate()
                {
                    while (true)
                    {
                        string command = Console.ReadLine();
                        switch (command)
                        {
                            case "close":
                                tf.Invoke(new MethodInvoker(
                                    delegate()
                                    {
                                        tf.Close();
                                    }));
                                break;
                            case "max":
                                tf.Invoke(new MethodInvoker(
                                    delegate()
                                    {
                                        tf.WindowState = FormWindowState.Maximized;
                                    }));
                                break;
                            case "min":
                                tf.Invoke(new MethodInvoker(
                                    delegate()
                                    {
                                        tf.WindowState = FormWindowState.Minimized;
                                    }));
                                break;
                            case "res":
                                tf.Invoke(new MethodInvoker(
                                    delegate()
                                    {
                                        tf.WindowState = FormWindowState.Normal;
                                    }));
                                break;
                        }

                    }
                }));
            ct.Start();
            tf.Show();
            tf.Activate();
            Application.Run(tf);
            Environment.Exit(0);
        }
    }
}
  • Save, Build and Run.

This program will open a console and a form. The console accepts commands that can control the behavior of the form. The commands are:

  • min: minimizes the window.
  • max: maximizes the window.
  • res: restores the window.
  • close: closes the window and exits.

The general idea is to have two threads in the program: one thread to manage the Form and one to manage the Console. I kept the the main thread as the one managing the graphical interface because I’m afraid that the attribute [STAThread] initializes the thread in order to interact with COM, for example during drag&drop events.

The Console interacts with the Form using the Invoke function because the Form runs in a different thread. I’m using anonymous functions for brevity. If you use C# 1.0 then you should replace them with delegate instances. The Form.Show and Form.Activate members are called to bring the form on top of the console. The Environment.Exit function is called to exit the program even if the console thread is still alive.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Posted in: Software