Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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": [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[/Code]

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Shad0wR1d3r
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


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 this post


Link to post
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.

More information on the subject in the SlimDX documentation.

Share this post


Link to post
Share on other sites
The app isn't terminating when you close the window because it's still running the infinite loop in your main function.
You could make some OnClose handler for your form and terminate the app from there, for example.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this