Click here to Skip to main content
15,889,096 members
Home / Discussions / C#
   

C#

 
GeneralRe: Application.Run()??? Pin
PIEBALDconsult15-Nov-09 15:10
mvePIEBALDconsult15-Nov-09 15:10 
GeneralRe: Application.Run()??? Pin
Luc Pattyn15-Nov-09 15:12
sitebuilderLuc Pattyn15-Nov-09 15:12 
GeneralRe: Application.Run()??? Pin
PIEBALDconsult15-Nov-09 15:23
mvePIEBALDconsult15-Nov-09 15:23 
GeneralRe: Application.Run()??? Pin
binhvtt15-Nov-09 15:09
binhvtt15-Nov-09 15:09 
GeneralRe: Application.Run()??? Pin
PIEBALDconsult15-Nov-09 15:24
mvePIEBALDconsult15-Nov-09 15:24 
GeneralRe: Application.Run()??? Pin
binhvtt15-Nov-09 16:50
binhvtt15-Nov-09 16:50 
GeneralRe: Application.Run()??? Pin
PIEBALDconsult15-Nov-09 17:02
mvePIEBALDconsult15-Nov-09 17:02 
AnswerRe: Application.Run()??? Pin
dojohansen15-Nov-09 23:06
dojohansen15-Nov-09 23:06 
Windows is basically a message-driven system. Unlike the programming model we get to play with, where we "receive events" when they occur, each application actually has to make system calls to get messages for the application from the system. These messages are generated by the OS and/or other programs and represent information such as a key changing state from up to down, the mouse moving, the program needs to redraw itself, and a gazillion other things.

So crudely put, and surely leaving out lots of detail, the Run() method displays the form passed to it, implements a loop that calls the Windows GetMessage() function and processes the messages in the queue. Messages received will be passed to relevant controls and the controls in turn raise events, providing us with a way of plugging our custom code into this loop. When the application receives a WM_CLOSE message or the form closes, the loop terminates and the Main method returns, terminating the program.

This is also why normal Windows Forms apps are terrible for implementing things like games. The GetMessage() function blocks when the message queue for an application is empty, which is very good when the application *is* event-driven and doesn't need to do anything other than in response to some external event - because it let's Windows allocate CPU to other programs that do have something in their message queue. Games however need to keep working and drawing and calculating object movements and so on even if there are no external events. They therefore implement the message loop a little differently, calling the system function PeekMessage() instead, which never blocks but returns immediately even if there are no messages. That's great for games, but it also explains why games are so unfriendly multi-taskers - they always take a lot of resources.

A digression if I may: Unfortunately this does make some seemingly simple things a bit tricky to implement in Windows Forms apps. For example, let's say you want to do something *while* a mouse button is pressed, as opposed to in response to the transition from up to down or down to up. You could do this with a timer (which is a WM_TIMER message - the lowest-priority message of all Windows Messages), but it wouldn't be very nice. You might have to start another thread doing the work when the button is pressed and cause it to stop when the button is released, but that's a bit complicated too, because the controls are not thread-safe so this other thread can't update anything in the UI, but instead has to marshal calls back to the UI thread to do the updating... Of course, if the control had an event that fired on every iteration of the program loop as long as the mouse button is pressed this would solve the problem, but generally the controls don't have any such events!
QuestionGet/use data in a c# program? Pin
ahlm15-Nov-09 9:44
ahlm15-Nov-09 9:44 
AnswerRe: Get/use data in a c# program? Pin
Abhishek Sur15-Nov-09 10:55
professionalAbhishek Sur15-Nov-09 10:55 
GeneralRe: Get/use data in a c# program? Pin
ahlm15-Nov-09 18:54
ahlm15-Nov-09 18:54 
GeneralRe: Get/use data in a c# program? Pin
Abhishek Sur15-Nov-09 21:02
professionalAbhishek Sur15-Nov-09 21:02 
GeneralRe: Get/use data in a c# program? Pin
Shameel16-Nov-09 3:04
professionalShameel16-Nov-09 3:04 
GeneralRe: Get/use data in a c# program? Pin
Abhishek Sur16-Nov-09 3:35
professionalAbhishek Sur16-Nov-09 3:35 
GeneralRe: Get/use data in a c# program? Pin
dojohansen15-Nov-09 23:16
dojohansen15-Nov-09 23:16 
GeneralRe: Get/use data in a c# program? Pin
ahlm16-Nov-09 6:25
ahlm16-Nov-09 6:25 
QuestionForm's height being cut off Pin
gdub15-Nov-09 9:17
gdub15-Nov-09 9:17 
AnswerRe: Form's height being cut off Pin
Luc Pattyn15-Nov-09 10:09
sitebuilderLuc Pattyn15-Nov-09 10:09 
GeneralRe: Form's height being cut off Pin
dojohansen15-Nov-09 23:20
dojohansen15-Nov-09 23:20 
Questionproblem in reading of connection string inside app.config by application Pin
Mohamed El-Wehishy15-Nov-09 8:58
Mohamed El-Wehishy15-Nov-09 8:58 
AnswerRe: problem in reading of connection string inside app.config by application Pin
PIEBALDconsult15-Nov-09 13:45
mvePIEBALDconsult15-Nov-09 13:45 
AnswerRe: problem in reading of connection string inside app.config by application Pin
Shameel16-Nov-09 3:08
professionalShameel16-Nov-09 3:08 
Questionsome Windows 7 and C# question Pin
E_Gold15-Nov-09 7:49
E_Gold15-Nov-09 7:49 
AnswerRe: some Windows 7 and C# question Pin
Christian Graus15-Nov-09 8:56
protectorChristian Graus15-Nov-09 8:56 
GeneralRe: some Windows 7 and C# question Pin
Adam Maras15-Nov-09 14:19
Adam Maras15-Nov-09 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.