Page 1 of 1

Silly problem with settimer_

Posted: Wed Sep 27, 2023 2:10 am
by ricardo
Hello,

I have being long away from PB and now im finding this silly problem.

When i click the button the GUI freezes.

I dont want to use Threads, i need to use some timer.

Maybe i am tired but can't find the solution, any help are welcome. Thanks

Code: Select all


Procedure Run1(nada)
  KillTimer_(WindowID(0),1)
  
  Debug "RUN1"
  Repeat
    Delay(1000)
    ilu+1
    Debug ilu
  Until a = 9
  
EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100



If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
    AddGadgetItem(10,-1,"1")
    AddGadgetItem(10,-1,"2")
    AddGadgetItem(10,0-1,"3")

    
    
    

  
  ;CloseGadgetList()
  
  Repeat
    Event = WaitWindowEvent(100)
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 32
           r = SetTimer_(WindowID(0),1,500,@Run1())
        EndSelect
        
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf

End

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 3:59 am
by RASHAD

Code: Select all

Procedure Run1(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_TIMER
    KillTimer_(WindowID(0),1)
    Repeat
      Delay(1000)
      ilu+1
      Debug ilu
    Until ilu >= 9
    SetGadgetText(31,"Finished")
  EndIf  
  ProcedureReturn result
EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100

If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
  AddGadgetItem(10,-1,"1")
  AddGadgetItem(10,-1,"2")
  AddGadgetItem(10,0-1,"3")
  
  Repeat
    Event = WaitWindowEvent(100)
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 32
            SetTimer_(WindowID(0),1,500,@Run1())            
        EndSelect
        
    EndSelect
  Until event = #PB_Event_CloseWindow
EndIf

End

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 5:01 am
by ricardo
Hi Rashad,

Thanks for your answer.
But if i change until for FOREVER, it continuing freezing

Code: Select all

Procedure Run1(hWnd, uMsg, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_TIMER
    KillTimer_(WindowID(0),1)
    Repeat
      Delay(10)
      ilu+1
      Debug ilu
      SetWindowTitle(0,Str(ilu))
    ForEver
    SetGadgetText(31,"Finished")
  EndIf  
  ProcedureReturn Result
EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100

If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
    AddGadgetItem(10,-1,"1")
    AddGadgetItem(10,-1,"2")
    AddGadgetItem(10,0-1,"3")
    
    Repeat
      Event = WaitWindowEvent(100)
      
      Select Event
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 32
              SetTimer_(WindowID(0),1,500,@Run1())            
          EndSelect
          
      EndSelect
    Until Event = #PB_Event_CloseWindow
  EndIf
  
  End

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 5:03 am
by ricardo
RASHAD wrote: Wed Sep 27, 2023 3:59 am

Code: Select all

Procedure Run1(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_TIMER
    KillTimer_(WindowID(0),1)
    Repeat
      Delay(1000)
      ilu+1
      Debug ilu
    Until ilu >= 9
    SetGadgetText(31,"Finished")
  EndIf  
  ProcedureReturn result
EndProcedure

Hi Rashad,

Thanks for your answer.
But if i change until for FOREVER, it continuing freezing

Code: Select all

Procedure Run1(hWnd, uMsg, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_TIMER
    KillTimer_(WindowID(0),1)
    Repeat
      Delay(10)
      ilu+1
      Debug ilu
      SetWindowTitle(0,Str(ilu))
    ForEver
    SetGadgetText(31,"Finished")
  EndIf  
  ProcedureReturn Result
EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100

If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
    AddGadgetItem(10,-1,"1")
    AddGadgetItem(10,-1,"2")
    AddGadgetItem(10,0-1,"3")
    
    Repeat
      Event = WaitWindowEvent(100)
      
      Select Event
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 32
              SetTimer_(WindowID(0),1,500,@Run1())            
          EndSelect
          
      EndSelect
    Until Event = #PB_Event_CloseWindow
  EndIf
  
  End

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 5:16 am
by RASHAD
You are running inside a loop
If you need to count the elapsed time all along without freezing the main window you may need a Thread

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 10:39 am
by RASHAD
Or

Code: Select all

Global ilu

Procedure Run1(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_TIMER
    Delay(10)
    ilu+1 
    SetWindowTitle(0,Str(ilu)) 
  EndIf  

EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100

If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
  AddGadgetItem(10,-1,"1")
  AddGadgetItem(10,-1,"2")
  AddGadgetItem(10,0-1,"3")
  
  Repeat
    Event = WaitWindowEvent(100)
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 32
            SetTimer_(WindowID(0),1,500,@Run1())            
        EndSelect
        
    EndSelect
  Until event = #PB_Event_CloseWindow
EndIf

End

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 11:00 am
by BarryG
Why are you not using the AddWindowTimer() command? Do you want something like this:

Code: Select all

Global ilu

Procedure Run1()
  ilu+1
  Debug ilu
EndProcedure

Result = ExamineDesktops()
ww = DesktopWidth(0)
hh = DesktopHeight(0)-100

If OpenWindow(0,0,0,ww,hh,"",#PB_Window_SystemMenu )
  TextGadget(30,10,7,100,25,"Enter")
  StringGadget(31,100,5,400,25,"")
  ButtonGadget(32,500,5,50,25,"GO")
  PanelGadget(10,0,30,WindowWidth(0),WindowHeight(0))
  AddGadgetItem(10,-1,"1")
  AddGadgetItem(10,-1,"2")
  AddGadgetItem(10,0-1,"3")
  
  Repeat
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 32
            RemoveWindowTimer(0, 0)
            ilu = 0
            Run1()
            AddWindowTimer(0, 0, 1000)
        EndSelect
        
      Case #PB_Event_Timer
        Run1()
        
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 1:01 pm
by infratec
In general and logical:

If you build an endless loop without a thread, your application freeze.
What should it do else ???

So you have to avid the endless loop.
2 possibilities:

1. Thread
2. Statemachine

Re: Silly problem with settimer_

Posted: Wed Sep 27, 2023 3:38 pm
by Axolotl
okay here are my 2 cents.....
Not only is it a working example, but it also shows some of my personal coding styles that might interest you.
With the other examples, you now have a variation of possible solutions....

Code: Select all

EnableExplicit  ; Axolotl: my personal style (always use this) 

Enumeration EWindow 	; Axolotl: much better than numbers (naming style is up to you) 
  #WINDOW_Main 
EndEnumeration

Enumeration EGadget 
  #GADGET_pnlMain 
  #GADGET_txtInfo 
  #GADGET_strInput 
  #GADGET_btnGo  	; formerly known as Button with Gadget #32 
EndEnumeration 

; or use at least constants like (Axolotl: I prefer enums, but constants are ok too.) 
; 
#TIMER_Test = 0   

Procedure BetterRun() 
  Static ilu = 0 

  ilu + 1 
  Debug ilu 

  If ilu >= 9  ; 
    ilu = 0   ; because static variable 
    RemoveWindowTimer(#WINDOW_Main, #TIMER_Test) 
    DisableGadget(#GADGET_btnGo, 0) 
  EndIf 
EndProcedure 


Procedure OnButtonClick()   
  Debug "RUN1"
  AddWindowTimer(#WINDOW_Main, #TIMER_Test, 500)     ; Axolotl: PB style 
  BindEvent(#PB_Event_Timer, @BetterRun(), #WINDOW_Main) 

  DisableGadget(#GADGET_btnGo, 1) 
EndProcedure


Procedure main() 
  Protected Event, count, ww, hh  
  
  count = ExamineDesktops() 
  ww = DesktopWidth(0)
  hh = DesktopHeight(0)-100

  If OpenWindow(#WINDOW_Main,0,0,ww,hh,"",#PB_Window_SystemMenu )
    TextGadget(#GADGET_txtInfo,10,7,100,25,"Enter")
    StringGadget(#GADGET_strInput,100,5,400,25,"")
    ButtonGadget(#GADGET_btnGo,500,5,50,25,"GO")

    PanelGadget(#GADGET_pnlMain,0,30,WindowWidth(0),WindowHeight(0))
      AddGadgetItem(#GADGET_pnlMain,-1,"1")
      AddGadgetItem(#GADGET_pnlMain,-1,"2")
      AddGadgetItem(#GADGET_pnlMain,-1,"3")
    CloseGadgetList()   ; PanelGadget 

    BindGadgetEvent(#GADGET_btnGo, @OnButtonClick()) 

    Repeat
      Event = WaitWindowEvent(100)  ; Axolotl: you do not use the timeout
      Select Event 
      ; Case #PB_Event_None         ; Axolotl: here the timeout (event) 
      ;   Debug "APP is in idle mode" 
      EndSelect
    Until Event = #PB_Event_CloseWindow
  EndIf 
  ProcedureReturn 0 
EndProcedure 

End main() 
Happy coding and stay healthy.