How to run winform from a console application?

How to create, execute and manage winform from a console application?

+51
c # winforms console-application
Nov 10 '08 at
source share
8 answers

The easiest option is to start the Windows Forms project, and then change the output type to the console application. Alternatively, just add a link to System.Windows.Forms.dll and start coding:

using System.Windows.Forms; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form()); // or whatever } 

An important bit is [STAThread] for your Main() method, which is necessary for full COM support.

+75
Nov 10 '08 at
source share

I recently wanted to do this and found that I was not happy with any of the answers here.

If you follow the advice of Marc and set the output type to the console application, there are two problems:

1) If you run the application from Explorer, you will get an annoying console window behind your form, which will not disappear until your program exits. We can mitigate this problem by calling FreeConsole before displaying the GUI (Application.Run). The annoyance here is that the console window still appears. He leaves immediately, but for a moment there is nothing.

2) If you started it from the console and displayed a graphical interface, the console is locked until the GUI exits. This is because the console (cmd.exe) believes that it should run console applications synchronously, and Windows applications asynchronously (the unix equivalent is "myprocess &").


If you exit the output type as a Windows application but call AttachConsole correctly, you do not get a second console window when called from the console, and you do not get the unnecessary console when called from Explorer. The proper way to call AttachConsole is to pass -1 to it. This forces our process to join the console of our parent process (the console window that started us).

However, this has two different problems:

1) Since the console launches Windows applications in the background, it immediately displays a prompt and allows you to enter additional data. On the one hand, this is good news, the console is not locked in your graphical application, but in the case when you want to output the output to the console and never show the graphical interface, the output of your program appears after the prompt, and no new prompt appears when you are done. This looks a bit confusing, not to mention the fact that your “console application” is running in the background and the user can execute other commands while working.

2) Flow redirection is also confused, for example. "myapp some options> somefile" cannot be redirected. Redirecting a stream requires a significant amount of p / Invoke to fix standard handles, but it is solvable.


After many hours of hunting and experimentation, I came to the conclusion that there is no way to do this perfectly. You simply cannot get all the benefits of a console and windows without any side effects. It is a matter of choosing which side effects are the least annoying for your application.

+26
Jun 15 '12 at 20:59
source share

Here is the best method I found: First set the output type of your projects to “Windows Application”, then P / Invoke AllocConsole to create a console window.

 internal static class NativeMethods { [DllImport("kernel32.dll")] internal static extern Boolean AllocConsole(); } static class Program { static void Main(string[] args) { if (args.Length == 0) { // run as windows app Application.EnableVisualStyles(); Application.Run(new Form1()); } else { // run as console app NativeMethods.AllocConsole(); Console.WriteLine("Hello World"); Console.ReadLine(); } } } 
+18
Nov 11 '08 at 2:03
source share

It is very simple:

Just add the following attribute and code to your Main method:

 [STAThread] void Main(string[] args]) { Application.EnableVisualStyles(); //Do some stuff... while(!Exit) { Application.DoEvents(); //Now if you call "form.Show()" your form won´t be frozen //Do your stuff } } 

Now you can show WinForms :)

+6
Dec 01 '11 at 20:26
source share

You can create a winform project in VS2005 / VS2008 and then change its properties as a command line application. It can then be launched from the command line, but it will still open winform.

+4
Nov 10 '08 at
source share

You should be able to use the application class in the same way as Winform applications. Probably the easiest way to start a new project is to do what Mark suggested: create a new Winform project, and then change it in the console application settings

0
Nov 10 '08 at
source share

It worked for my needs ...

 Task mytask = Task.Run(() => { MyForm form = new MyForm(); form.ShowDialog(); }); 

This starts from a new thread and does not release the thread until the form is closed. Task is in .Net 4 and later.

0
Jul 09 '15 at 3:29
source share

It completely depends on your choice, how you implement.
but. Attached process, for example: form input and console printing
b. An independent process, for example: starting a timer, do not close, even if the console output.

for a,

 Application.Run(new Form1()); //or ------------- Form1 f = new Form1(); f.ShowDialog(); 

for b, Use a thread or ask something, How to open a win form yourself?

0
Aug 22 '15 at 11:03
source share



All Articles