0

Create secondary thread



Hi, Is it possible to create a secondary thread in an autoplay.brs file? Specifically I have a the Main program loop wait for and process messages from a message port. On one specific message I'd like to create secondary thread running an infinte loop process. The main control would then return the message port loop when at some later time another user input would trigger code to kill off the previously created secondary thread. thanks Andrew

3 comments

  • 0
    Avatar
    kbenson


    It's not exactly what you asked for, but it should be possible to get similar behavior (if your secondary loop is doing work in small discrete units) by setting a small timeout on your wait() call, and calling out to a function that processed a small number of these discrete work units.  E.g.


    while true
       msg = wait(100,port)
       if msg <> invalid
           ' do main loop stuff here
       end if
       do_sub_loop_work()
    end while


    Essentially, every tenth of a second you'll call do_sub_loop_work() regardless if a msg was sent, and you'll only process a msg if it was actually sent.

    If you have a large set of operations to perform in the secondary "thread" that don't split into discrete chunks easily, this approach may not work well.

    P.S.  Sorry if any code doesn't quite match, I'm just peeking in from the Roku DVP Developer forum and from what I understand the versions of BrightScript may have some differences.
  • 0
    Avatar
    Andrew Gray


    Hi There,

    thanks for the reply. Developer minds must think alike <!-- s;-) --><img src="{SMILIES_PATH}/icon_wink.gif" alt=";-)" title="Wink" /><!-- s;-) --> this is actually what I implemented as a 'workaround' while waiting to find out if there is any support for secondary threads, I guess there isn't?

    It work's ok for what I needed to do at this time, although not so elegant. What I did to smarten it up just a little was use a variable for the message port timeout, set it to 0 when I'm not wanting to do my sub-processing and to the timeout I required (1s) when doing the 'second thread' work.

    cheers
    Andrew
  • 0
    Avatar
    kbenson


    Makes sense.  Glad to hear you got it worked out!
Please sign in to leave a comment.