• 10
• 13
• 19
• 27
• 9

# Real Time Loop in VB 2008 Just to see if i can

This topic is 2941 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Funny enough I though I've done it and by "done it" I mean I have created a loop with a wierd message handler an endless loop and a window that "responds". Anyway, here's my problem number 1 XD: Because the way the messages are handled I have no idea how to handle them manually so I can actually close the whole app when I close the window (hopefully someone can shed some light on that plz :D). My problem number 2: Is when I try to see how fast the loop is updating I get crazy results something like 46929 updates every second (or is that normal) so my guess is that my way of measuring the updates is wrong. Anyway's here's my "code": Imports System.Windows.Forms Module RealTime_module Public Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long Public Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long 'Window class is just a Windows Form with a textbox on it Dim Wnd As New Window() Dim FpsCount As Integer Dim Counter As System.Diagnostics.Stopwatch Sub Main() timeBeginPeriod(1) Counter = System.Diagnostics.Stopwatch.StartNew() Wnd.Show() While True FPS() Application.DoEvents() End While Counter.Stop() timeEndPeriod(1) End Sub Public Sub FPS() If Counter.ElapsedMilliseconds >= 1000 Then 'txtFps is a textbox on the form called Wnd Wnd.Controls.Item("txtFps").Text = FpsCount.ToString Counter.Reset() FpsCount = 0 Else FpsCount += 1 End If End Sub End Module

##### Share on other sites
I'm not sure what you're trying to achieve here, so I'll give a couple notes.

Firstly, framerates are commonly* calculated 'instantaneously' - calculate the reciprocal of the time between every two consecutive frames (so record a 'last' time, a 'now' time and set fps = 1 / (now - last) every frame). This has the downside of not being so well averaged - it'll jump around a heck of a lot, so really your method is just a 'smoothed' version of it.

Secondly, I assume you think that the 'framerates' you're getting are far too high. They're not - think about what you're actually doing. The overwhelming majority of the iterations consist of a single conditional and an increment. You're not doing very much each frame, so you can fit lots of them into one second.

Thirdly, remember that framerate is 1 / time. The difference between 50000 fps and 100 fps is about the same as the difference between 100 fps and 50 fps.

* read: this is how I always do them, and assume others are similar

##### Share on other sites
hmmm I'll change the counter, and yes it is a small amount of instructions, but the amount of frames is still way too large
btw this is how I always calcuate my fps and I seemed to work so far

##### Share on other sites
Quote:
 Original post by Shad0wR1d3rhmmm I'll change the counter, and yes it is a small amount of instructions, but the amount of frames is still way too large

No, it really isn't. That works out to about 20 microseconds per frame, which sounds about right. I can't emphasise enough that each iteration is doing virtually nothing whatsoever, and your processor is running at a ridiculously fast rate - so you can fit quite a lot of iterations in. Everything involved will fit in cache, so you're not even waiting on memory.

Quote:
 btw this is how I always calcuate my fps and I seemed to work so far

That's what I'm saying - it will work, I was just giving an alternative. Sorry if that wasn't clear.

##### Share on other sites
Incidentally, using Application.DoEvents isn't recommended for a real application. There are allocations going on in the background, and you really don't want to be adding unnecessary pressure to the GC for a real time application.