tags:

views:

329

answers:

3

I am going out of my skull trying to figure this out. 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(). Everywhere I have looked I have found absolutely nothing that will help me. I am using Visual C#'s gui designer. Please help, as this is the only thing preventing me from moving on in my project and again, everywhere I have looked has left me dry.

EDIT: Problem solved with an instance of Timer.

+12  A: 

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
Not sure where the GetMessage API info comes in, though...
sheepsimulator
sheepsimulator: What do you mean? Application.Run establishes a message loop. Internally, it calls the `GetMessage` Win32 API function in a loop.
Mehrdad Afshari
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
You'd use a timer for this, and add some text every time the timer triggers.
Eclipse
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
Alright, any examples on it's usage in that manner? Or maybe I could figure it out from here. Thanks for your help.
Orm
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
@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.
280Z28
I still need to know how to do this for reasons other than Sockets.
Orm
@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.
280Z28
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
+4  A: 

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.

280Z28
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
You don't. You add your program's logic into event handlers.
Eclipse
A: 

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

RCIX