none
Timer as a solution to FCExtension.Async... RRS feed

  • General discussion

  • Most of us want to get the most out the default Small Basic Library. No extensions. But it comes to be a little difficult when we need multitasking. A controller, an animator... So, most os us, think that we NEED to use FCExtensions async methods.

    Well, it comes out that, is You attach a sub into the Timer, resume it => the running sub will be running async. If You make the sub work for a longer time (Program.delay) then You will see few threads running async.

    Let's play

    tickID = 0
    
    Timer.Interval = 1000
    Timer.Tick = Writer
    Timer.Resume()
    
    Sub Writer
      tickID = tickID + 1
      
      While "True"
        TextWindow.WriteLine("I have ticked - tick ID " + tickID)
        
        Program.Delay(2500)  
      EndWhile 
    endsub

    and as wek can see, messages sgow up faster than 2.5 second, so that means, that there are many threads running async.

    Saturday, February 25, 2012 2:34 PM

All replies

  • It's true that almost all "advanced" programs do need proper threading.

    Timer starts always new async thread on Tick. For example TimerPlus in Data Extension has little slightly different functionality.

    With "converted" version of your code tick ID is always 1 because program gets stuck to endless loop.

    My solution to threading error was creation of Threading object (currently in beta state, haven't finished testing) when I get stuck with same problem in my game project.

    It's just little complicated to access shared variables and subs from different threads.

    Currently my API for threading is

    • create
    • destroy
    • start
    • stop
    • join

    But I will probably include some thread properties like priority and state.

    Saturday, February 25, 2012 4:32 PM
  • What about adding threads into Small Basic?

    For those, who know NCQ for RXT (Lego Mindstorms), found that their solution for threads (tasks) is really simple.

    A Task class.

    -Start(SubName, Identifier)

    -IsRunning(Identifier)

    -Stop(Identifier)

    -WaitUntilEnds(Identifier)

    -GetAllTasks() => results a array tasks["1"] = "firstIdentifier", tasks["2"] = "second" ..

    It's simple?

    Wednesday, February 29, 2012 12:25 PM