Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
ioan
Level 7

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:

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
0 Kudos
4 Replies
joetesta
Level 10

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
0 Kudos
ioan
Level 7

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 like 

m.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 Smiley Happy
https://github.com/e1ioan/
http://rokucam.com
0 Kudos
ioan
Level 7

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 like 

m.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
0 Kudos
joetesta
Level 10

Re: Terminate a Task gracefully

sorry it didn't work Smiley Sad
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
0 Kudos