Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Nov 15, 2018 10:28 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Speed of Thread
PostPosted: Sun May 13, 2012 3:48 pm 
Offline

Joined: Thu May 03, 2012 3:21 pm
Posts: 5
Location: Switzerland
My thread has this code:
dim LoopSpeed = 100 '(ms)
dim UDPThreadRequestStop as boolean
Dim Now as double
dim TimeCounter as Uint32
UDPThreadRequestStop = false
Now = Microseconds
do until UDPThreadRequestStop =true
app.doevents
MainSemaphore.Signal
if Microseconds-Now >= (LoopSpeed * 1000) then
Now = Microseconds
'Code to execute every 100ms
if Timecounter >= 10000 then UDPThreadRequestStop = true 'Exit Loop
'Other code....... executed in 8 ms
TimeCounter = TimeCounter + LoopSpeed
end if
MainSemaphore.release
loop


The thread ends after 100 loops (100ms * 100 = 10000ms) but it ends only after 11 or 12 seconds (not 10 seconds).
If I remove the code "app.doevents", the thread works perfectly but the CPU Usage is 30%.

There is a solution to this problem?
Why does "app.doevents" need a lot of CPU time?

Note: The project contains only a thread and a window.


Top
 Profile  
Reply with quote  
 Post subject: Re: Speed of Thread
PostPosted: Sun May 13, 2012 4:08 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
DoEvents is really for Console Applications so you don't need need it, nor does it really do anything in desktop applications. Threads in RB apps are cooperative meaning that it will play nice with other threads so the timing may or may not be (probably not) be very accurate or reliable.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Speed of Thread
PostPosted: Sun May 13, 2012 7:53 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
When DoEvents is called from a Thread, it yields to the main thread so that the Event Loop can be run. This necessarily slows down any code that calls DoEvents, especially within a loop where the delay is repeated in each iteration.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Speed of Thread
PostPosted: Sun May 13, 2012 7:55 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Your timing code is not very accurate and is susceptible to "drift". Instead of adding LoopSpeed to a counter, compare the total elapsed time. The code that adds LoopSpeed to TimeCounter could execute after 100ms, or 120ms, or more, allowing the total actual time to drift longer and longer.

If you want to get as close to 10 seconds as possible, use
if Microseconds - Now >= 10000000 then UDPThreadRequestStop = true


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group