"TheEndless" wrote:"RokuJoel" wrote:
Rather than using sleep to control event timing, as sleep() and wait() suffer from the same issue, consider using several roTimeSpan objects to trigger events at a specific duration
Assuming you mean inside a while loop (can't think of any other way to track it), without a sleep in there, that sounds like a really good way to lock up the box, particularly if you're not listening for any events. But, if you add wait in there to catch any events (and to give the processor some breathing room), you're right back to square one.
while <looping-condition>
msg = msgport.GetMessage()
if type(msg) = ......
<process message>
end if
<other loop stuffis>
end while
Function MyWait(ms as Integer, ThePort as Object) as Object
t = CreateObject("roTimeSpan")
result = ThePort.GetMessage()
while ms >= 0 and result = invalid and (t.TotalMilliseconds() < ms or ms = 0)
result = ThePort.GetMessage()
end while
return result
End Function
Sub MySleep(ms as Integer)
t = CreateObject("roTimeSpan")
while t.TotalMilliseconds() < ms and ms > 0
end while
End Sub
"TheEndless" wrote:
that sounds like a really good way to lock up the box
"destruk" wrote:
Would using a goto work?
The use of goto is discouraged in C programming, and some authors of C programming books claim that the goto statement is never necessary, but used judiciously, it can simplify certain programs. The reason that many programmers frown upon the use of goto is that with the unrestrained use of goto statements, it is easy to create a program with undefined program flow, which can never be debugged.
With that said, there are instances where a goto statement can come in handy, and simplify coding. One of these situations is to break out of deeply nested for loops, or if logic blocks, on a certain condition.
for(byte r = 0; r < 255; r++){
for(byte g = 255; g > -1; g--){
for(byte b = 0; b < 255; b++){
if (analogRead(0) > 250){ goto bailout;}
// more statements ...
}
}
}
bailout:
"RokuJoel" wrote:"TheEndless" wrote:
that sounds like a really good way to lock up the box
I haven't seen while loops locking the box, even empty ones, I use them all the time when testing, instead of using stop, to prevent test code from exiting when it completes. Since he'll be checking the message port, just without a wait(), it should be exitable if he puts in a handler for remote keypress.
"MazeWizzard" wrote:
However, the question remains... why even use Wait() or sleep()? There may be a reason... but I don't know what it is (since I don't have their code... can't see the internals). Maybe it does give the processor a break... or give the IR/RF sensors time. Or something else entirely. I guess the question becomes... What do we lose? What's it doing now that it won't do if we omit Wait()?
sub main()
screen=createobject("roScreen")
port=screen.getport()
timer=createobject("rotimespan")
while true
timer.mark()
msg=port.getmessage()
?"time: ";timer.totalmilliseconds()
?"message: ";msg
end while
end sub
"RokuJoel" wrote:
<snip>
Freeing up 10-20ms by eliminating the wait() on the Roku2 platform, and even the 1-2 ms on the "Classic" Roku platform is significant from a game developer perspective where every millisecond counts.
<snip>
"RokuJoel" wrote:
Since Brightscript is not multi-threaded, I don't think you need to worry about thread locking at this point.
"MazeWizzard" wrote:
I still don't know what Wait() is doing for those 10-15 ms
"TheEndless" wrote:
<snip>
My guess would be that it's doing a GetMessage() loop with a 1ms sleep. That would explain why a Wait(1) and a Sleep(1) produce the same ~10ms delay.
We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. You will not be able to log in or post new comments or kudos during this time. Read more here.
Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 12 and you may notice reduced functionality.
In the meantime, for additional assistance, visit our Support Site.
Thanks for your patience — we’re excited to share what’s next!