0

I am using c# and have a console application. Now, to integrate GUI, I added a windows form application. Thing is, the windows form and the console application need to communicate but the functions built inside the console app don't work till the windows form is closed.

Its like the windows form is overlapping it.

  • 1
    How are the console application and the windows form application related? Are they in the same project, or separate projects? Can you give some example code? – pmcoltrane Jun 10 '14 at 14:36
  • What do you mean by "they need to communicate" and "the functions (...) don't work"? If you wan't to debug 2 programs at the same time, you're going to have to have 2 instances of visual studio running. – Rik Jun 10 '14 at 14:37
  • You can also check the discussion here: http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application – Itai Bar-Haim May 02 '16 at 06:24

3 Answers3

0

You can control the lunch behaviour by simply going to Solution property page, Common properties,Start up project. Over there you can specify which program to run first in the action section.

MHOOS
  • 5,146
  • 11
  • 39
  • 74
0

From what you write I think I understand that you write your console application code after Application.Run(new Form1()); and so your code probably looks like this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        //Some more things to do here
    }
}

This is not the way to go. a project can be either a console app. or a winforms project. If you need them both to run together , use two projects. If you need them to communicate , use a communication object, such as WCF framework server/client.

Itai Bar-Haim
  • 1,686
  • 16
  • 40
omer schleifer
  • 3,897
  • 5
  • 31
  • 42
0

Create a new console project and add a reference to System.Windows.Forms. Add a new form to your project. As an example, I added one button to the form and set it's DialogResult to Ok.

In your program's main method, create an instance of your form and open it using Show or ShowDialog. Here's an example:

static void Main(string[] args)
{
    Console.WriteLine("Opening window...");

    var result = new TestForm().ShowDialog();

    if (result == System.Windows.Forms.DialogResult.OK)
        Console.WriteLine("Form closed by button.");
    else
        Console.WriteLine("Form closed otherwise.");

    Console.ReadLine();
}

From your form any location within your program you can use the static Console class to access the console. Here's an example that prints some status info from the form's constructor:

public partial class TestForm : Form
{
    public TestForm()
    {
        InitializeComponent();

        Console.WriteLine("Form initialized.");
    }
}

If you already have created your project as class library or windows forms project, right-click it, navigate to properties and switch the "Output type" to "Console application".

Carsten
  • 11,287
  • 7
  • 39
  • 62