Velius

I've figured out how to use the Application.Idle event handler. The only problem I am having is that the application is by default *waiting* for messages before going to idle like usual. When I used the Win32 API i alwasy made my program bypass to idle time if there was no message by using PeekMessage(). How do I get my C# application to behave this way. I can't hope to make a game function properly if it *waits* for messages before idling. Any help would be appreciated Smile Also to note, I just recently adopted C#. I'm a diehard C++ guy. Hate some of it's limitations (no default parameters ) but I like it's demand for cleanliness.

Color is an integer that starts off at 0 and is supposed to cycle to white. so the window should be very colorful and changing with each idle loop.

Code used to tranform and integer into a Color:

Color IntToColor(int c)

{

int r, g, b;

r = c >> 16;

g = (c >> 8) - (r << 8);

b = c - (g << 8) - (r << 16);

return Color.FromArgb(0, r, g, b);

}

My code:

Application.Idle += new EventHandler(MainForm_Idle);

// As stated above this handler won't execute until some sort of other event raises (mouse, keyboard, etc)

void MainForm_Idle(object sender, EventArgs e)

{

gfx.Clear(color);

gfx.Swap();

if(color < 0xffffff) color++;

}



Re: Windows Forms General How do I get a true idle loop with c# in .NET?

nobugz

The equivalent of PeekMessage() is Application.DoEvents(). You could modify your Main() function in Program.cs like this:

[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 f1 = new Form1();
f1.FormClosed += QuitLoop;
f1.Show();
do {
Application.DoEvents();
// Do your stuff to implement the main game loop
//...
// Yield the CPU
System.Threading.Thread.Sleep(1);
} while (!mQuit);
}
private static bool mQuit;
private static void QuitLoop(object sender, FormClosedEventArgs e) {
mQuit = true;
}

Use Sleep(0) if you want all the CPU cycles you can get.





Re: Windows Forms General How do I get a true idle loop with c# in .NET?

Velius

That looks very much like the kind of loop control I'm use to. The help is very much appreciated Smile



Re: Windows Forms General How do I get a true idle loop with c# in .NET?

Ðãv? S. Â???????

Use Sleep(0) if you want all the CPU cycles you can get.


Could you explain to me what you mean by that I'm rather curious.





Re: Windows Forms General How do I get a true idle loop with c# in .NET?

nobugz

Sleep(0) only yields the CPU if there is another thread ready to run. CPU utilization will jump to 100%. Sleep(1) always yields, CPU utilization is bounded.





Re: Windows Forms General How do I get a true idle loop with c# in .NET?

Ðãv? S. Â???????

Thanks.