How to make the pause button work in below program

Just starting out? Need help? Post your questions and find answers here.
Allen
User
User
Posts: 92
Joined: Wed Nov 10, 2021 2:05 am

How to make the pause button work in below program

Post by Allen »

Hi,

In below code, when I press the PAUSE button, the program did not pause. Any suggestion?

Thanks

Allen

Code: Select all

Enumeration FormWindow
  #Main
  #Start
  #Pause
EndEnumeration

Procedure Events(event)
  Define.b stop
  Define.i i
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Start
          Repeat
            i=12 ; main operation here
          Until stop
          MessageRequester("Pause","Pause pressed")
          Stop=#False
        Case #Pause
          Stop=#True
      EndSelect
  EndSelect
EndProcedure

MainWindow=OpenWindow(#Main, 100, 100, 100, 100, "Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
ButtonGadget(#Start,10,10,50,20,"Start"):ButtonGadget(#Pause,10,30,50,20,"Pause")

Repeat 
  event = WaitWindowEvent()
  Events (event)
Until event = #PB_Event_CloseWindow 
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 341
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: How to make the pause button work in below program

Post by Mindphazer »

First thing i see, when you click on Start, you enter an infinite loop :

Code: Select all

Repeat
  i = 12
until stop
Stop never assigns a value, so you never leave the loop

Second point, your MessageRequester should be inside Case #Pause lik this :

Code: Select all

Case #Pause
    MessageRequester("Pause","Pause pressed")
    Stop=#True
EndSelect
Last edited by Mindphazer on Wed Dec 08, 2021 3:24 pm, edited 1 time in total.
MacBook Pro 14" M1 Pro - 16 Gb - MacOS 14 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to make the pause button work in below program

Post by skywalk »

You set the stop variable only in the pause button.
The start gui is in an infinite loop.

Search for mk-soft, infratec examples of PostEvent() using custom messages.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to make the pause button work in below program

Post by mk-soft »

Never stop or interrupt the EventLoop, otherwise no more events will be processed.
Also do not use Delay(), otherwise the events will jam.

To process something served in the MainLoop, you can work via Windows Timer, or outsource it in a thread. With threads one must consider however one more.

See example: Mini Thread Control

Code: Select all


Enumeration FormWindow
  #Main
  #Start
  #Pause
EndEnumeration

Procedure DoAny()
  Static cnt
  cnt + 1
  Debug cnt
EndProcedure

Procedure Events(event)
  Define.b stop
  Define.i i
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Start
          Stop=#False
          AddWindowTimer(#Main, 1, 1000)
          
        Case #Pause
          Stop=#True
          RemoveWindowTimer(#Main, 1)
      EndSelect
      
    Case #PB_Event_Timer
      Select EventTimer()
        Case 1
          DoAny()
          
      EndSelect
      
  EndSelect
EndProcedure

MainWindow=OpenWindow(#Main, 100, 100, 100, 100, "Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
ButtonGadget(#Start,10,10,50,20,"Start"):ButtonGadget(#Pause,10,30,50,20,"Pause")

Repeat 
  event = WaitWindowEvent()
  Events (event)
Until event = #PB_Event_CloseWindow 
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Allen
User
User
Posts: 92
Joined: Wed Nov 10, 2021 2:05 am

Re: How to make the pause button work in below program

Post by Allen »

Thanks everyone for all the advice. I shall study and learn from your examples.

Allen
Post Reply