Proper Loop Management

BlitzMax Forums/MaxGUI Module/Proper Loop Management

Ked(Posted 2016) [#1]
In regards to a single-threaded GUI application, would it be beneficial to the system if a Delay was thrown into the main loop to help not hog CPU resources?
Repeat
     If AppTerminate() = True Exit

     If TApp.Process() = 0 Exit

     Delay 10
Forever
TApp.Free()
End
Or am I just being ridiculous? :D

Thanks!


RustyKristi(Posted 2016) [#2]
Still on topic, I'm having a similar problem and yes I use single thread. I got this game loop where sometimes it hangs even when I have AppTerminate and Exit


Kryzon(Posted 2016) [#3]
Instead of Delay(), use WaitEvent().
It halts the main thread of the program until a system event is received by the application.

Repeat

	WaitEvent()

	'For the program to reach this part it means the WaitEvent() call above has received one or more events.

	Select EventID()
		Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE
			Exit
	End Select

Forever



Ked(Posted 2016) [#4]
Oh, sorry. I forgot to mention that I'll be using PollEvent().

Edit: But, this is just a general question.


Kryzon(Posted 2016) [#5]
Polling implies having a frequency (how often do you poll for events?).
If you poll too fast you consume CPU \ notebook battery.
If you poll too slow, the program may feel sluggish for the user.

Use a timer frequency of 30 FPS, it's very lightweight on the CPU but is still interactive (you can have animated graphics at this rate etc.). If you need something faster, 60 FPS should do.

Instead of using the default BRL timers with WaitTimer() (which uses WaitSystem internally, if I'm not mistaken), it's more lightweight to use a timer that directly calls your polling function.
An example: http://www.blitzbasic.com/codearcs/codearcs.php?code=3138


Ked(Posted 2016) [#6]
Awesome. Sounds good. I was just wondering what would be "proper."

Thanks, Kryzon. It's been a bit since I've wandered these forums. I wasn't sure where to look for the answer. And, the search was not helpful.