events - probably a very stupid question and easily solved

Just starting out? Need help? Post your questions and find answers here.
coffee
User
User
Posts: 77
Joined: Fri Oct 06, 2017 10:43 am

events - probably a very stupid question and easily solved

Post by coffee »

hi,

i have a program with a very long running procedure, that is called by pressing a button (goButton).
how do i keep the window responsive and the cancel button functional?

in the demo the window freezes an cancel will not cancel or be reachable. thank you for any help and explanations in advance.

here a dumb demo

Code: Select all

EnableExplicit

Procedure Cleardummy()
  Protected i.i, max=1000, d.i=0
  For i = 0 To max
    d+i
  Next  
EndProcedure

Procedure dummyInternal()
  Protected i.i, max=100000, d.i=0
  Protected j.i, max1=100, d1.i=0
  For i = 0 To max
    d+i
    For j = 0 To max1
      d1+j
    Next
    d1=0
  Next  
EndProcedure

Procedure.i readdemo(x = 0, y = 0, width = 436, height = 132)
  Protected readdemo1, readnewevent, numfilesstring.s
  Protected displaynumText, readProgressBar, goButton, CancelButton
  Protected Iteration.i = 0, j.i = 100
  Protected updated.i=0
  readdemo1 = OpenWindow(#PB_Any, x, y, width, height, "readdemo", #PB_Window_SystemMenu)
  If readdemo1
    displaynumText = TextGadget(#PB_Any, 12, 38, 412, 16, "", #PB_Text_Center)
    readProgressBar = ProgressBarGadget(#PB_Any, 12, 60, 412, 25, 0, 0)
    goButton = ButtonGadget(#PB_Any, 12, 96, 152, 25, "Go")
    CancelButton = ButtonGadget(#PB_Any, 272, 96, 152, 25, "Cancel", #PB_Button_Default)
    SetGadgetText(displaynumText, "Insert " + Str(0) + " of "+ 0)
    Repeat
      readnewevent = WaitWindowEvent()
      Select readnewevent
        Case #PB_Event_CloseWindow
          ProcedureReturn 0
        Case #PB_Event_Gadget
          Select EventGadget()
            Case CancelButton
              Break            
            Case goButton
              DisableGadget(goButton, 1)
              numfilesstring = Str(j)
              SetGadgetAttribute(readProgressBar, #PB_ProgressBar_Maximum, j)
              While updated <= j
                Cleardummy()                  
    	          dummyInternal()	          
    		        Iteration + 1
    		        SetGadgetText(displaynumText, "Insert " + Str(Iteration) + " of "+ numfilesstring)
                SetGadgetState(readProgressBar, Iteration)
                ResizeGadget(readProgressBar,#PB_Ignore,#PB_Ignore,#PB_Ignore,#PB_Ignore)
                updated+1
              Wend
          EndSelect
      EndSelect  
    ForEver
  EndIf
  CloseWindow(readdemo1)
  ProcedureReturn Iteration
EndProcedure

readdemo()

End
coffee
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: events - probably a very stupid question and easily solv

Post by Fig »

Check events in your long loop, that's the secret...
At least, discard them and display a message to make user wait until the end of the loop.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
coffee
User
User
Posts: 77
Joined: Fri Oct 06, 2017 10:43 am

Re: events - probably a very stupid question and easily solv

Post by coffee »

thanks,

do you have a pointer to some demo for an imbecil like me?
coffee
User
User
Posts: 77
Joined: Fri Oct 06, 2017 10:43 am

Re: events - probably a very stupid question and easily solv

Post by coffee »

I tried the below, but it didn't do the trick


Code: Select all

        If Event()
           If windownum = EventWindow()
             If cancelgadget = EventGadget()
               ProcedureReturn #True
             EndIf
           EndIf
         EndIf 
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4667
Joined: Sun Apr 12, 2009 6:27 am

Re: events - probably a very stupid question and easily solv

Post by RASHAD »

It is better in your case to use Thread
Fast implementation :wink:

Code: Select all

;EnableExplicit

Global numfilesstring.s,goButton,readProgressBar,CancelButton,displaynumText,Thread,cancel

Procedure Cleardummy()
  Protected i.i, max=1000, d.i=0
  For i = 0 To max
    d+i
  Next 
EndProcedure

Procedure dummyInternal()
  Protected i.i, max=100000, d.i=0
  Protected j.i, max1=100, d1.i=0
  For i = 0 To max
    d+i
    For j = 0 To max1
      d1+j
    Next
    d1=0
  Next 
EndProcedure

Procedure goButtonCB(par)
  Iteration = 0
  j = 100
  updated=0
  DisableGadget(goButton, 1)
  numfilesstring = Str(j)
  SetGadgetAttribute(readProgressBar, #PB_ProgressBar_Maximum, j)
  While updated <= j
     If cancel = 1
        KillThread(Thread)
     EndIf
     Cleardummy()                 
     dummyInternal()            
      Iteration + 1
      SetGadgetText(displaynumText, "Insert " + Str(Iteration) + " of "+ numfilesstring)
    SetGadgetState(readProgressBar, Iteration)
    ;ResizeGadget(readProgressBar,#PB_Ignore,#PB_Ignore,#PB_Ignore,#PB_Ignore)
    updated+1
  Wend
EndProcedure

Procedure.i readdemo(x = 0, y = 0, width = 436, height = 132)
  ;Protected readdemo1, readnewevent
  readdemo1 = OpenWindow(#PB_Any, x, y, width, height, "readdemo", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If readdemo1
    displaynumText = TextGadget(#PB_Any, 12, 38, 412, 16, "", #PB_Text_Center)
    readProgressBar = ProgressBarGadget(#PB_Any, 12, 60, 412, 25, 0, 0)
    goButton = ButtonGadget(#PB_Any, 12, 96, 152, 25, "Go")
    CancelButton = ButtonGadget(#PB_Any, 272, 96, 152, 25, "Cancel",#PB_Button_Default )
    SetGadgetText(displaynumText, "Insert " + Str(0) + " of "+ 0)   
    
    Repeat
      readnewevent = WaitWindowEvent()
      Select readnewevent
        Case #PB_Event_CloseWindow
          ProcedureReturn 0
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case CancelButton
              cancel = 1
                        
            Case goButton
                Thread = CreateThread(@goButtonCB(),35)
;               DisableGadget(goButton, 1)
;               numfilesstring = Str(j)
;               SetGadgetAttribute(readProgressBar, #PB_ProgressBar_Maximum, j)
;               While updated <= j
;                  Debug GetGadgetState(CancelButton)
;                  Cleardummy()                 
;                  dummyInternal()            
;                   Iteration + 1
;                   SetGadgetText(displaynumText, "Insert " + Str(Iteration) + " of "+ numfilesstring)
;                 SetGadgetState(readProgressBar, Iteration)
;                 ResizeGadget(readProgressBar,#PB_Ignore,#PB_Ignore,#PB_Ignore,#PB_Ignore)
;                 updated+1
;               Wend
          EndSelect
      EndSelect 
    ForEver
  EndIf
  CloseWindow(readdemo1)
  ProcedureReturn Iteration
EndProcedure

readdemo()

End
Egypt my love
coffee
User
User
Posts: 77
Joined: Fri Oct 06, 2017 10:43 am

Re: events - probably a very stupid question and easily solv

Post by coffee »

RASHAD thanks million, this works great for me!


coffee
Post Reply