Needing a wee bit of help

Just starting out? Need help? Post your questions and find answers here.
Evil1
User
User
Posts: 43
Joined: Fri Jul 15, 2016 8:55 pm

Needing a wee bit of help

Post by Evil1 »

I have written a program that sends a password (or text) to a Window :-

Image

It works as intended but with a slight issue ...

I was trying to be clever and have the send button disable and grey out when pressed, this was so that after pressing the send button, the program would move on to the main procedure mainprog() to actually send the password and stop the send button being pushed multiple times. The problem was that the button did not grey out or disable until the mainprog() finished and returned to the main waitwindow event, the only way I could come up with a solution was to introduce a loop delay, that would loop within the waiteventwindow to give time for the window to redraw before moving on to the mainprog() procedure. This does not work on all systems, timing, CPU etc.

Code: Select all

If Event = #PB_Event_CloseWindow Or GetGadgetState(Button_1)=1 Or sent>=lpdel
  
  DisableGadget(button_0,0)
  SetGadgetState(button_0,0)
  
   tb.s=""
  
  MainProg() ; call main program procedure that kicks of the actual sending of the password
  
EndIf
Other wise program loops within waitwindow event.

Basically I am really struggling to have the send button grey out and disable before moving on to the mainprog() and then re-enable.

Any ideas, I hope I am making sense :-)

I have this at the start of the waitwindow loop :-

Code: Select all

Event = WaitWindowEvent() ;Wait for a Window event
    If sent>0 And sent<lpdel ;sent is set to 1 if Send button is pressed or hotkey key is used, this starts off a counter from 1 to the value of lpdel (This loop delay forces the main window loop routine to repeat (only fix I could come up with to make the gadget update state in the window before exiting main loop to run mainprog()
      sent=sent+1
    EndIf
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Needing a wee bit of help

Post by Demivec »

Perhaps have your button event code disable the button and post a custom event for the main window event loop. In that event loop catch the custom event and call MainProg() before reenabling the button.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Needing a wee bit of help

Post by skywalk »

Don't think of your spawned program as MainProg().
The primary window of your app and its event loop is Main().
Interrupting that event loop will be noticed by the user.
So, your worker function should be started as a thread and it should PostMessage() back to the gui.
This way your event loop never stalls.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Needing a wee bit of help

Post by RASHAD »

Try

Code: Select all

AddWindowTimer(#win,100,10) ;10 ms timeut

Repeat

    Case #PB_Event_Timer
      If pressed = 1
        DisableGadget(#button,1)
        Run + 1
        If Run >= 10 ;10*10 = 100 ms use 
          DisableGadget(#button,0)
          pressed = 0
          Run = 0
        EndIf
      EndIf           
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #button
          pressed = 1  ;you may need pressed to be global var.
          ;Send your password          
          
Until .....
Egypt my love
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Needing a wee bit of help

Post by chi »

Evil1 wrote:Basically I am really struggling to have the send button grey out and disable before moving on to the mainprog() and then re-enable.
Is following example working for you?

Code: Select all

OpenWindow(0, 0, 0, 200, 150, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ButtonGadget(0, 50, 55, 100, 30, "click me")

Repeat  
  event = WaitWindowEvent() 
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case 0          
          DisableGadget(0, 1)          
          While WindowEvent()
            Delay(15)
          Wend          
          Delay(2000) ; <- your MainProg() goes here          
          DisableGadget(0, 0)
          
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
Et cetera is my worst enemy
Evil1
User
User
Posts: 43
Joined: Fri Jul 15, 2016 8:55 pm

Re: Needing a wee bit of help

Post by Evil1 »

chi wrote:
Evil1 wrote:Basically I am really struggling to have the send button grey out and disable before moving on to the mainprog() and then re-enable.
Is following example working for you?

Code: Select all

OpenWindow(0, 0, 0, 200, 150, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ButtonGadget(0, 50, 55, 100, 30, "click me")

Repeat  
  event = WaitWindowEvent() 
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case 0          
          DisableGadget(0, 1)          
          While WindowEvent()
            Delay(15)
          Wend          
          Delay(2000) ; <- your MainProg() goes here          
          DisableGadget(0, 0)
          
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
Perfect, just what I was after - now works as intended. I should have called mainprog() from within the waitwindow event, the way I was trying to do it was after exiting the waitwindow event :-)

Thanks for all the advice, appreciated.
Post Reply