none
Program.Delay does not work in Button Pressed Sub RRS feed

  • Question

  • Hi,

    I encountered a problem: After pressing a button "text1" should be drawn and 2 seconds later "text2" should be drawn. However in the sub OnButtonPressed text1 and text2 are shown at the same time - both AFTER the 2 seconds of program.delay. If I use the same code outside this sub it works as it should. Is it a bug of SmallBasic or do I have a wrong conception of ButtonClicked and the way text is treated in Graphicswindow?

    Thanks for any help

    Button1=Controls.AddButton("Button1",10,20)
    Controls.ButtonClicked=OnButtonClicked
     GraphicsWindow.DrawText(100,100,"Text1")  '----This works
     Program.Delay(2000)
     GraphicsWindow.DrawText(100,200,"Text2")  
    Sub OnButtonClicked
      If Controls.LastClickedButton=Button1 Then
         GraphicsWindow.DrawText(200,100,"Text1")     '--- This doesn't work
         Program.Delay(2000)
         GraphicsWindow.DrawText(200,200,"Text2")  
       EndIf
    EndSub

    Sunday, January 14, 2018 10:09 PM

Answers

  • Stuff done inside event threads only update once the event has completed and the UI thread is active again.

    So rule is just set flags in event subroutine and do all work on the main UI thread in a 'game loop'

    See below link for more details

    https://social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    Your code could therefore be:

    Button1=Controls.AddButton("Button1",10,20)
    Controls.ButtonClicked=OnButtonClicked
    GraphicsWindow.DrawText(100,100,"Text1")  '----This works
    Program.Delay(2000)
    GraphicsWindow.DrawText(100,200,"Text2")  
    
    'Infinite game loop handling events
    While ("True")
      If (clicked) Then
        clicked = "" 'Event handled
        If (Controls.LastClickedButton=Button1) Then     
          GraphicsWindow.DrawText(200,100,"Text1")     '--- This doesn't work
          Program.Delay(2000)
          GraphicsWindow.DrawText(200,200,"Text2")  
        EndIf
      EndIf
      
      Program.Delay(20) 'Don't over mash cpu
    EndWhile
    
    Sub OnButtonClicked
      clicked = "True"
    EndSub 


    Sunday, January 14, 2018 10:16 PM
    Moderator

All replies

  • Stuff done inside event threads only update once the event has completed and the UI thread is active again.

    So rule is just set flags in event subroutine and do all work on the main UI thread in a 'game loop'

    See below link for more details

    https://social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    Your code could therefore be:

    Button1=Controls.AddButton("Button1",10,20)
    Controls.ButtonClicked=OnButtonClicked
    GraphicsWindow.DrawText(100,100,"Text1")  '----This works
    Program.Delay(2000)
    GraphicsWindow.DrawText(100,200,"Text2")  
    
    'Infinite game loop handling events
    While ("True")
      If (clicked) Then
        clicked = "" 'Event handled
        If (Controls.LastClickedButton=Button1) Then     
          GraphicsWindow.DrawText(200,100,"Text1")     '--- This doesn't work
          Program.Delay(2000)
          GraphicsWindow.DrawText(200,200,"Text2")  
        EndIf
      EndIf
      
      Program.Delay(20) 'Don't over mash cpu
    EndWhile
    
    Sub OnButtonClicked
      clicked = "True"
    EndSub 


    Sunday, January 14, 2018 10:16 PM
    Moderator
  • Thanks a lot for the quick answer and the link.

    Gery

    Monday, January 15, 2018 7:21 AM