ioan
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018
10:37 AM
Terminate a Task gracefully
I'm using a Task to read very fast from the socket and I have some problems with terminating the task.
Right now my code looks something like this:
As you can see, the loop is infinite, the only time it ends is when the user requests it (back button on the remote), so I terminate the task with
This works fairly good, but the problem is that the socket remains connected and the peer doesn't know that the connection ended.
Another solution I tried is this:
... and then to terminate the task I set the
The problem is that this solution is VERY slow. Lets say I get 30 loops a second in the "while true", the "while not m.top.terminate" will reduce the speed by half or more.
What's the best way to terminate a Task so I can close the socket connection while keeping the speed high?
Right now my code looks something like this:
sub init()
m.top.functionName = "readFromSocket"
end sub
sub readFromSocket()
socket = CreateObject("roStreamSocket")
...
while true
...
read very fast and process
end while
end sub
As you can see, the loop is infinite, the only time it ends is when the user requests it (back button on the remote), so I terminate the task with
m.task.control = "stop"
This works fairly good, but the problem is that the socket remains connected and the peer doesn't know that the connection ended.
Another solution I tried is this:
sub init()
m.top.functionName = "readFromSocket"
end sub
sub readFromSocket()
socket = CreateObject("roStreamSocket")
...
while not m.top.terminate
...
read very fast and process
end while
if socket.isConnected() then
socket.close()
end if
end sub
... and then to terminate the task I set the
m.task.terminate = true
The problem is that this solution is VERY slow. Lets say I get 30 loops a second in the "while true", the "while not m.top.terminate" will reduce the speed by half or more.
What's the best way to terminate a Task so I can close the socket connection while keeping the speed high?
https://github.com/e1ioan/
http://rokucam.com
http://rokucam.com
4 REPLIES 4
joetesta
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018
11:14 AM
Re: Terminate a Task gracefully
Instead of forcing the loop to look at an "m.top" variable each time through, maybe you could use a local variable that gets updated based on an observer on the m.top var. Not sure whether it'd be more efficient but something like
m.top.observeField("terminate","terminateLoop")
sub terminateLoop()
m.flag = 1
end sub
m.flag = 0
while m.flag <1
aspiring
ioan
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018
11:15 AM
Re: Terminate a Task gracefully
"joetesta" wrote:
Instead of forcing the loop to look at an "m.top" variable each time through, maybe you could use a local variable that gets updated based on an observer on the m.top var. Not sure whether it'd be more efficient but something likem.top.observeField("terminate","terminateLoop")
sub terminateLoop()
m.flag = 1
end sub
m.flag = 0
while m.flag <1
Thank you. I'll test and report back 🙂
https://github.com/e1ioan/
http://rokucam.com
http://rokucam.com
ioan
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018
02:32 PM
Re: Terminate a Task gracefully
"joetesta" wrote:
Instead of forcing the loop to look at an "m.top" variable each time through, maybe you could use a local variable that gets updated based on an observer on the m.top var. Not sure whether it'd be more efficient but something likem.top.observeField("terminate","terminateLoop")
sub terminateLoop()
m.flag = 1
end sub
m.flag = 0
while m.flag <1
It doesn't work, I don't know why... the value of m.flag never changes in the loop. The application gets to terminateLoop() but m.flag in the while never changes. To test, I changed the example from here: https://sdkdocs.roku.com/download/attachments/1612025/SimpleTask.zip?version=1&modificationDate=1460215524154&api=v2
Here is the modified code:
https://sites.google.com/site/marginall ... ects=0&d=1
https://github.com/e1ioan/
http://rokucam.com
http://rokucam.com
joetesta
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018
06:03 PM
Re: Terminate a Task gracefully
sorry it didn't work 😞
You have to avoid the m.top lookup to avoid the performance hit, don't know if you can handle the keypress inside the loop but suspect it'd have the same issues as m.flag, never get picked up while it's looping.
You have to avoid the m.top lookup to avoid the performance hit, don't know if you can handle the keypress inside the loop but suspect it'd have the same issues as m.flag, never get picked up while it's looping.
aspiring