Windows Form vs Console Application
Any idea on how does Windows Form stay even after its execution unless we hit on the close [X] mark explicitly. This is开发者_开发知识库 not the case for Console Application?
I would like to know how this is managed by .NET internally?
Thanks in advance
In order to get this difference I believe you should read something about Windows Messages
(Messages Queue
, Message loop
, etc.). For example here. .Net doesn't bring anything new here, it's more about windows applications itself.
You are mistaken: a Windows Forms program only stays during its execution and does not remain after it. It only stops executing after you exit the program, for example by clicking the close [X] button.
Do you mean to ask what the difference is between console and windows applications, and why they execute differently when started from the command line? This is controlled by a flag in the (PE) executable header, which is set by the /subsystem:console or /subsystem:windows option of the linker. It is a totally insane method in my opinion; Unix uses a much more logical method for this (the user can specify himself whether the shell waits for it or not).
This is the same for both .NET and native executables.
Well WinForms have more code than just what you type. They don't close write away because the code is in a loop where it constantly checks for events, and if the event Close is triggered then they will close.
Imagine that you code something like this in your Console Application:
while(true)
{
Console.WriteLine("Enter a Number: ");
int x;
Console.Read(X);
if(x==-1) break;
Console.Write(x*x);
}
The loop would repeat forever until a -1 is entered, you could say that something similar goes on with winforms. Of course this is an oversimplification but imagine something like that.
If you'd like to learn more you should research about Windows Messages (Messages Queue, Message loop
as Snowbeard said
精彩评论