What makes an app console or Windows Form applicat

2020-03-09 07:19发布

[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?

4条回答
来,给爷笑一个
2楼-- · 2020-03-09 07:52

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

查看更多
等我变得足够好
3楼-- · 2020-03-09 08:06

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.

查看更多
Summer. ? 凉城
4楼-- · 2020-03-09 08:10

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>
查看更多
老娘就宠你
5楼-- · 2020-03-09 08:15

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.

查看更多
登录 后发表回答