Silly problem with settimer_

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Silly problem with settimer_

Post 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
ARGENTINA WORLD CHAMPION
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5014
Joined: Sun Apr 12, 2009 6:27 am

Re: Silly problem with settimer_

Post 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
Egypt my love
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Silly problem with settimer_

Post 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
ARGENTINA WORLD CHAMPION
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Silly problem with settimer_

Post 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
ARGENTINA WORLD CHAMPION
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5014
Joined: Sun Apr 12, 2009 6:27 am

Re: Silly problem with settimer_

Post 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
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5014
Joined: Sun Apr 12, 2009 6:27 am

Re: Silly problem with settimer_

Post 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
Egypt my love
BarryG
Addict
Addict
Posts: 4272
Joined: Thu Apr 18, 2019 8:17 am

Re: Silly problem with settimer_

Post 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
Last edited by BarryG on Thu Sep 28, 2023 8:30 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7706
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Silly problem with settimer_

Post 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
Axolotl
Addict
Addict
Posts: 901
Joined: Wed Dec 31, 2008 3:36 pm

Re: Silly problem with settimer_

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply