views:

1250

answers:

4

[Visual Studio 2008]

I created a new project for console application and modified it to look like this:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}

Then I created another project for Windows Form application and modified it:


static class Program
{
    //[STAThread] commented this line
    static void Main (string[] args) { //Added args
        //Commented following lines
        //Application.EnableVisualStyles ();
        //Application.SetCompatibleTextRenderingDefault (false);
        //Application.Run (new Form1 ()); commented this line
        Thread.Sleep (2000);
    }
}

Now I have neither written Console functions (Console.Write etc.) in first application nor I have written forms related operations in second one. Looks identical to me.

Still first application shows BLACK window and second one doesn't show anything. What makes it work like this?

+13  A: 

If you inspect the exe files usine ILDASM you can see that there is a difference in the Manifest (look for "subsystem").

In a Winforms application:

.subsystem 0x0002       // WINDOWS_GUI

In a console application:

.subsystem 0x0003       // WINDOWS_CUI

There may be more differencies in the IL code.

When it comes to what makes the compiler emit this differently in the two cases, this is controlled by the project file's OutputType value:

In a Winforms application:

<OutputType>WinExe</OutputType>

In a console application:

<OutputType>Exe</OutputType>

Out of curiosity I also checked that value for a Class Library project:

<OutputType>Library</OutputType>
Fredrik Mörk
I get your point but my question is precisely this: What makes Visual studio (or compiler) create this difference? Anything in application must be governed from the code we write. Right?
Hemant
For the compiler there is no difference, it all has to do with the assembly type you've set in the project settings and the references.
Gerrie Schenck
Added update with project file differencies.
Fredrik Mörk
As a result of these different settings, the generated output will be different, and Windows will treat the application differently at start up. I guess you could say that the 'OutputType' element is the 'code' that drives this distinction.
jerryjvl
+6  A: 

In project properties, Application Tab, Output Type you can set to 'Windows Application' or 'Console Application'.

I believe that behind the scenes VS does exactly what Fredrik presented in his post.

Also, setting it to Console Application will show you the black console application for the windows Forms project.

AlexDrenea
For completeness there is also an option to have output to a Class Library.
RichardOD
+2  A: 

If you look in the project file (csproj) you'll see that the target is defined there as either a console or windows app.

jerryjvl
+2  A: 

Under the bonnet, there is no difference in a winform vs console exe except for a flag in the PE-header that says "I need a console". The PE header is not controlled from your C# (since it is a compile thing, not a runtime thing), so this is defined in the project file instead (<OutputType>...</OutputType>).

Or at the command-line (csc /target:exe vs csc /target:winexe).

Arguably, they could have used an assembly-level attribute that the compiler intercepted - but would that really have helped? Probably not.

Marc Gravell