tags:

views:

135

answers:

2

Hello Everybody

I was thinking that WinForms are classes as any other classes. But starting form project get me confuse. Why new Form start with Application.Run and what kind of mechanism happen inside of it ?

+8  A: 

As it says in the documentation, it begins running a standard application message loop on the current thread. The message loop handles clicks, keypresses, paint requests, plus many other things.

When called with a form as an argument it also makes that form visible.

Mark Byers
Application.Run works like bridge between OS and Form class ? But how background mechanism work ? Application.Run use some Form class'es methods (send messages arguments) ?
Freshblood
Normally i expected Form does his own jobs natively but it looks it leaves some jobs for Application.Run
Freshblood
A typical form doesn't do anything until an event is raised. Without the message loop the events would not be raised so nothing would happen. Try showing a form without calling `Application.Run(form);`. The form will appear but it will be totally unresponsive. It doesn't even paint correctly. The same thing happens if you write blocking code in an event handler - if you block the main thread you prevent the message loop from running so your form doesn't receive any more events.
Mark Byers
@Freshblood - it's an old win32 api thing. It hooks your program into the win32 messaging loop, so that you can get messages (handle events) posted by the operating system. Since all mouse and keystroke events (and most anything else initiated by the user) come this way, you kind of need it.
Joel Coehoorn
Hmm... that was an answer in it's own right. Promoting...
Joel Coehoorn
+1  A: 

This goes way back to the Win32 API. Winforms sits on top the win32 api. Application.Run() plugs your program into (implements) the win32 message loop. Pretty much all mouse and keyboard events (and most anything else initiated by the user) are really win32 messages.

Joel Coehoorn