none
Parallel Processing and events Small Basic RRS feed

  • Question

  • Hello, I've just been playing around with the button click and timer event. My aim was to look for ways to run multiple infinite loops simultaneously.

    The first test sample uses timer and it seems to fire off each (3 in total) process simultaneously using interval = 0. Running parallel to each other.

    http://smallbasic.com/program/?FXD982  Runs in browser.

    The 2nd test sample uses button click and it seems to queue up the unique button events in series to each other and run parallel to the main program only. FCW408 Runs local.

    My question is are the above observations and conclusions accurate and complete?

    Also why wont the animation run smooth in the button click subroutine? It's very jerky.

    And finally could an array be used to create the subroutines for the timer sample?

    Thanks for any more insight on this.

    Monday, July 22, 2013 9:23 PM
    Moderator

Answers

  • I've explored SB's parallel programming on some occasions.

    But my best example is a game adaptation from a very old book.

    SB's TextWindow display can't read from keyboard w/o halting the whole program.

    So, action games are impossible on it w/o using parallel threads thru' Timer!

    You can check it out here:  KTX387-0

    death-valley-how-to-overcome-sbs-lack-of-inkey-function-for-textwindow-console-mode

    multiple-tasks-in-small-basic

    However, the programming language w/ easiest concurrent programming is Scratch in my opinion!  :D


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Monday, July 22, 2013 11:39 PM
    Answerer
  • Jibba  do you want to do this?

    SetGW()
    Controls.ButtonClicked = Click

    While "true"
      Animation()
    EndWhile

    Sub Click
      NN=NN+1
      X = 200
      Y = 5
      pace[NN] = 0.8
      movingBall[NN] = Shapes.AddEllipse(20,25)
      Shapes.Move(movingBall[NN], X+Math.GetRandomNumber(20), Y+50*(NN-1))
      sX[NN] =shapes.GetLeft( movingBall[NN])
      backLight[NN] = Shapes.AddRectangle(200, 25)
      Shapes.SetOpacity(backLight[NN], 10)
      Shapes.Move(backLight[NN], X, Y+50*(NN-1))
    EndSub

    Sub Animation
          For j=1 To NN
          sX[j] =shapes.GetLeft( movingBall[j]) + pace[j]
          Shapes.Move(movingBall[j], sX[j], Y+50*(j-1))
          If sX[j]> 378 Or sX[j] < 200 Then
            pace[j] = -pace[j]
          EndIf
        endfor
          Program.Delay(5)
     EndSub

    Sub SetGW
      w = 600
      h = 350
      GraphicsWindow.Width = w
      GraphicsWindow.Height = h
      GraphicsWindow.BrushColor = "blue"
      Controls.AddButton("Add",10,50)
    EndSub


    Tuesday, July 23, 2013 1:07 AM
    Answerer
  • Show KHR240

    Best Regards Martin

    Friday, July 26, 2013 6:19 PM

All replies

  • I've explored SB's parallel programming on some occasions.

    But my best example is a game adaptation from a very old book.

    SB's TextWindow display can't read from keyboard w/o halting the whole program.

    So, action games are impossible on it w/o using parallel threads thru' Timer!

    You can check it out here:  KTX387-0

    death-valley-how-to-overcome-sbs-lack-of-inkey-function-for-textwindow-console-mode

    multiple-tasks-in-small-basic

    However, the programming language w/ easiest concurrent programming is Scratch in my opinion!  :D


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Monday, July 22, 2013 11:39 PM
    Answerer
  • Jibba  do you want to do this?

    SetGW()
    Controls.ButtonClicked = Click

    While "true"
      Animation()
    EndWhile

    Sub Click
      NN=NN+1
      X = 200
      Y = 5
      pace[NN] = 0.8
      movingBall[NN] = Shapes.AddEllipse(20,25)
      Shapes.Move(movingBall[NN], X+Math.GetRandomNumber(20), Y+50*(NN-1))
      sX[NN] =shapes.GetLeft( movingBall[NN])
      backLight[NN] = Shapes.AddRectangle(200, 25)
      Shapes.SetOpacity(backLight[NN], 10)
      Shapes.Move(backLight[NN], X, Y+50*(NN-1))
    EndSub

    Sub Animation
          For j=1 To NN
          sX[j] =shapes.GetLeft( movingBall[j]) + pace[j]
          Shapes.Move(movingBall[j], sX[j], Y+50*(j-1))
          If sX[j]> 378 Or sX[j] < 200 Then
            pace[j] = -pace[j]
          EndIf
        endfor
          Program.Delay(5)
     EndSub

    Sub SetGW
      w = 600
      h = 350
      GraphicsWindow.Width = w
      GraphicsWindow.Height = h
      GraphicsWindow.BrushColor = "blue"
      Controls.AddButton("Add",10,50)
    EndSub


    Tuesday, July 23, 2013 1:07 AM
    Answerer
  • Hi goto

    I don't know if you remember but one of your replies (way back) is what 1st got me thinking about parallel "threads" (gives me a better understanding of how to use the word thread).

    Totally appreciate your answers they always seem to show/hint how by sample then explain using terminology which sets me on a path to understanding how the process works.

    That game runs really well and the above forum thread you gave is unreal. That preamble about how you arrived at writing that program is really informative to me. It sort you gives tips on how study programing among other things.

    I'll get around to experienting with the other events in the GW too.

    I'm going to set aside a day this month and go over my old forum threads and create a homework/to-do list, including that sample you gave on how the program.argument method works.

    Great reply as usual, thanks heaps.

    Tuesday, July 23, 2013 2:07 AM
    Moderator
  • Thanks NaochanON, that's exactly what I wanted to do.

    Amazing. Like the getLeft method.

    I'm learning so much here. Your samples have been unreal. Thanks again.

    Tuesday, July 23, 2013 2:29 AM
    Moderator
  • Oh nearly forgot.

    Does anyone know why the animation doesn't run smooth in the buttonClick event?

    Tuesday, July 23, 2013 2:34 AM
    Moderator
  • Jibba  do you want to do this?

    SetGW()
    Controls.ButtonClicked = Click

    While "true"
      Animation()
    EndWhile

    Sub Click
      NN=NN+1
      X = 200
      Y = 5
      pace[NN] = 0.8
      movingBall[NN] = Shapes.AddEllipse(20,25)
      Shapes.Move(movingBall[NN], X+Math.GetRandomNumber(20), Y+50*(NN-1))
      sX[NN] =shapes.GetLeft( movingBall[NN])
      backLight[NN] = Shapes.AddRectangle(200, 25)
      Shapes.SetOpacity(backLight[NN], 10)
      Shapes.Move(backLight[NN], X, Y+50*(NN-1))
    EndSub

    Sub Animation
          For j=1 To NN
          sX[j] =shapes.GetLeft( movingBall[j]) + pace[j]
          Shapes.Move(movingBall[j], sX[j], Y+50*(j-1))
          If sX[j]> 378 Or sX[j] < 200 Then
            pace[j] = -pace[j]
          EndIf
        endfor
          Program.Delay(5)
     EndSub

    Sub SetGW
      w = 600
      h = 350
      GraphicsWindow.Width = w
      GraphicsWindow.Height = h
      GraphicsWindow.BrushColor = "blue"
      Controls.AddButton("Add",10,50)
    EndSub


    This solution could become a TechNet Wiki article and enter the July Small Basic Guru contest: http://social.technet.microsoft.com/wiki/contents/articles/18211.technet-guru-contributions-july-2013.aspx

    Great job!


    Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Tuesday, July 23, 2013 6:26 AM
    Owner
  • Show KHR240

    Best Regards Martin

    Friday, July 26, 2013 6:19 PM
  • Thanks Martin, that's a great solution. Really good logic.
    Friday, July 26, 2013 9:39 PM
    Moderator
  • Some observations:

    • you can use Timer in both textWindow and GW, interestng
    • click is parallel to main then in series to each click event, queing
    • timer runs parallel to main and parallel to each event

    Thanks again for the samples. Very helpful.


    Friday, July 26, 2013 10:03 PM
    Moderator
  • re article: Cheers, thanks for the tip.
    Friday, July 26, 2013 10:40 PM
    Moderator