tags:

views:

2735

answers:

5

This is a minor bug (one I'm willing to live with in the interest of go-live, frankly), but I'm wondering if anyone else has ideas to fix it.

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

I'd like to not display the console window. (Like I said, it's a minor bug.)

The project output is already set to Windows Application.

Here's (most of) the code for the Main() method. I've snipped out various proprietary/security related stuff, replacing it with comments where appropriate.

[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();
        }
    }
}

Anyone have any ideas?

+4  A: 

My first guess would be to double check your Project Property settings and make sure that the output type is Windows Application and not Console Application.

Nick
Good thought; and I stupidly didn't check that first. However ... Nope, output is indeed Windows Application.
John Rudy
Output was Windows Application in VS; but not at raw compile. This is closest to the right answer.
John Rudy
+1  A: 

If you navigate to the Properties tabs for your project in Visual Studio, you should be able to set the output type to Windows Application. It sounds like it may be set to Console Application currently.

Ben Hoffstein
A: 

As above, check to make sure that your project's properties are set to Windows Application instead of Console Application. If that's not the issue, then a component of your application might be manually creating a console window using the Win32 API call AllocConsole(), or you might be launching a command-line application in the background without using UseShellExecute=false; CreateNoWindow=true; in your StartInfo.

Stephen Deken
+1  A: 

Take a backup of your code and then hack at it, removing everything unrelated to this issue. In other words, have a cycle of "remove code, get it to build, run it and see if the console still pops up." Eventually you should end up either spotting the issue or being able to post a short but complete program so that we can reproduce it and help fix it.

Jon Skeet
+4  A: 

I found it.

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

When the project is built from CruiseControl, that's when we get a console window.

The difference? Visual Studio (based on my election of a WinForms app) is appending /target:winexe to the csc line.

CruiseControl calls a series of NAnt scripts. In the source.build script, the compile step is misconfigured, and is targeting exe instead of winexe -- the equivalent of selecting "Console App" in VS. Thus, a console window on release builds vs. debug builds.

Relevant NAnt:

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

Yeah, now I feel dumb. :)

John Rudy