6

I have a Windows Form app in C# and am trying to figure out where I would implement the CLI app equivalent of the primary while loop in main(). I am using Visual C#'s gui designer.

EDIT: Problem solved with an instance of Timer.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Orm
  • 507
  • 6
  • 13
  • 3
    I suspect you're thinking about this at the wrong level. You might as well be asking "where in my C# program" the data for some polygon is moved to the GPU over some bus. Those ideas are not concepts in the C# language, those are implementation details that libraries, languages and runtime abstract away for you. The whole point of windows forms in C# is to ensure that you never have to think about message loops again. Yes, event-driven programs are built out of windows messages; sometimes the abstraction leaks, but you shouldn't need to talk directly at the message level in most normal cases. – Eric Lippert Sep 01 '09 at 22:14
  • @Orm: The example you linked to is very poorly written code. If you use it, I recommend you use it as a reference for what to avoid. – Sam Harwell Sep 01 '09 at 22:16
  • I am, and yes this is a crap way to do it. I tried, and it didn't work well at all. – Orm Sep 01 '09 at 22:29
  • If you know what you are trying to accomplish, you might search for or ask another question about that specifically, now that you know it's not what you originally thought. Without those details, it's hard to say more than what Eric said (which is generally true for any of his replies). – Sam Harwell Sep 01 '09 at 22:34

3 Answers3

13

It's inside the Application.Run method. You shouldn't call GetMessage API function directly:

// MyForm is derived from `System.Windows.Forms.Form` class
Application.Run(new MyForm()); 
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • sheepsimulator: What do you mean? Application.Run establishes a message loop. Internally, it calls the `GetMessage` Win32 API function in a loop. – Mehrdad Afshari Sep 01 '09 at 21:53
  • But then how do I add my programs logic into it. For example, if I wanted the program to continuously print "It works!" to a widget (adding text I have figured out, widget.Text+="It works!";). – Orm Sep 01 '09 at 21:53
  • You'd use a timer for this, and add some text every time the timer triggers. – Eclipse Sep 01 '09 at 21:55
  • The `Form` class and controls you add to it raise events. You handle those events and do stuff accordingly. You can look at the code generated by the Visual C# form designer in the `MyForm.designer.cs` file. – Mehrdad Afshari Sep 01 '09 at 21:56
  • Alright, any examples on it's usage in that manner? Or maybe I could figure it out from here. Thanks for your help. – Orm Sep 01 '09 at 21:57
  • The problem is that I am trying to make the application check data in a sockets buffer. IU just didnt have any clue where to impliment the loop to do what would normally be done in this manner: [code] void main() { while(true) { //program runs here } } – Orm Sep 01 '09 at 21:59
  • @Orm: You don't need or want to do that check in a .NET application because you have asynchronous I/O available (Socket.BeginReceive, Socket.BeginSend are two examples) which are superior in every way. – Sam Harwell Sep 01 '09 at 22:06
  • I still need to know how to do this for reasons other than Sockets. – Orm Sep 01 '09 at 22:29
  • @Orm: For whatever task you come up with, there's almost surely a cleaner way to do it. A cursory glance at the thought process "sockets? main loop. ui? main loop..." leaves one hoping for a sane way to accomplish things. If you're having trouble finding out how the .NET Framework was built to help you with a task, I'm sure that either 1) someone else already had the same question or 2) people here would be glad to point you in the right direction. Just find a way to express your current task and we'll go from there. – Sam Harwell Sep 01 '09 at 22:51
  • My current task was to understand how I can implement the regular CLI version of a main wile loop. The problem has been resolved, although I am sure that what you say is true. There is either a cleaner or more efficient way to do what I did. That will be a question for another time though. For now, this works, so I'm good. – Orm Sep 01 '09 at 23:06
4

It's entered from Application.Run(Form). You don't enter any logic in that loop. If you need to respond to input, add event handlers to the particular events to the controls on your form. If you need to run logic periodically, use one of the Timer classes.

The primary outcome of logic in the message pump in C++ is excess/unnecessary usage of the battery on laptops. You should definitely start rethinking the actual code requirements for meeting your target goal, and they shouldn't include constantly running logic in a spin loop.

Community
  • 1
  • 1
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Alright, that helps me out a bit, but now I need to know how to add my programs logic into that loop. That is what I am trying to figure out. – Orm Sep 01 '09 at 21:51
  • You don't. You add your program's logic into event handlers. – Eclipse Sep 01 '09 at 21:57
0

try reading this. The language isnt too clear, and the code is VB but it should get the point across.

RCIX
  • 38,647
  • 50
  • 150
  • 207