The console window is displayed when starting WinForm (C #)

This is a small mistake (I'm ready to live in the interests of go-live, to be honest), but I wonder if anyone has any ideas to fix this.

I have a C # WinForms application. When the application is launched through the executable file (not through the debugger), the first thing the user sees is the console window, followed by the main window (after pre-loading it is completed.)

I would not want to display a console window. (As I said, this is a small mistake.)

The project output is already installed in the Windows application.

Here (most) is the code for the Main () method. I cut out various material related to property / security, replacing them with comments where necessary.

[STAThread] static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // SNIP: Get username from Windows, associate with DB user if (user == null || user.UID == 0 || (user.Active.HasValue && !(user.Active.Value))) { MessageBox.Show(ErrorStrings.UnknownUser, ErrorStrings.TitleBar, MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); return; } // SNIP: Associate user with employee object Application.Run(new MainForm()); } catch (Exception ex) { if (ExceptionPolicy.HandleException(ex, UiStrings.ExceptionPolicy)) { string message = ErrorStrings.UnhandledPreface + ex.ToString(); MessageBox.Show(message, ErrorStrings.TitleBar, MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } } 

Does anyone have any ideas?

+4
source share
5 answers

My first suggestion would be to double check your project settings and make sure the output type is a Windows application, not a console application.

+4
source

I found him.

When the project is built in Visual Studio, there are no problems - there is no console window.

When a project is built from CruiseControl, this is when we get a console window.

Difference? Visual Studio (based on my choice of WinForms application) adds / target: winexe to the csc line.

CruiseControl invokes a series of NAnt scripts. In the source.build script, the compilation step is incorrectly configured and targets exe instead of winexe - the equivalent of choosing "Console Application" in VS. Thus, the console window in the release builds against the debug build.

Corresponding NAnt:

 <csc output="${build.outputPath}\[myapp].exe" target="winexe" debug="Full" rebuild="true"> <!-- lots of references, sources and resources --> </csc> 

Yes, now I feel stupid. :)

+4
source

If you go to the "Properties" tabs for your project in Visual Studio, you must set the output type to the Windows application. It looks like a console application can be installed at the moment.

+1
source

As stated above, make sure your project properties are set in the Windows application instead of the console application. If this is not a problem, then your application component can manually create a console window using the Win32 AllocConsole() API call, or you can run the application from the command line in the background without using UseShellExecute=false; CreateNoWindow=true; UseShellExecute=false; CreateNoWindow=true; in StartInfo.

+1
source

Make a backup of your code and then hack it by deleting everything that is not related to this problem. In other words, you have the cycle "delete the code, get it to build, run it and see if the console pops up." In the end, you must either identify the problem or publish a short but complete program so that we can reproduce it and help fix it.

+1
source

Source: https://habr.com/ru/post/1277534/


All Articles