PB5.41LTS(x86); Windows7; SetActiveGadget; CreateThread;

Windows specific forum
HanPBF
Enthusiast
Enthusiast
Posts: 564
Joined: Fri Feb 19, 2010 3:42 am

PB5.41LTS(x86); Windows7; SetActiveGadget; CreateThread;

Post by HanPBF »

Hello!

I have this code as extended from PB help:

Code: Select all

EnableExplicit
 
 Procedure	doSomething(P.i)
 	disablegadget(5, #True)
        debug "doSomething"
        DisableGadget(5, #False)
 	SetActiveGadget(5) ; this doesn't work
 EndProcedure
 
 If OpenWindow(0, 0, 0, 270, 240, "SetActiveGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget  (0, 10, 10, 250, 20, "bla bla...")
    ComboBoxGadget(1, 10, 40, 250, 21)
    StringGadget  (5, 10, 70, 250, 20, "done")
    
    define a
    
    For a = 1 To 5 : AddGadgetItem(1, -1, "ComboBox item " + Str(a)) : Next
    SetGadgetState(1, 2)                ; set (beginning with 0) the third item as active one
    ButtonGadget  (2, 10,  100, 250, 20, "Activate StringGadget")
    ButtonGadget  (3, 10, 130, 250, 20, "Activate ComboBox")
    ButtonGadget  (4, 10, 160, 270, 20, "do something")
    Repeat
      define Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget
        Select EventGadget()
          Case 2 
          	SetActiveGadget(0)   ; Activate StringGadget
          Case 3 
          	SetActiveGadget(1)   ; Activate ComboBoxGadget
          case 4
               
          	CreateThread(@doSomething(), 0)
                ; SetActiveGadget(5) ; this would work, but I want to await the thread...
        EndSelect
      EndIf
    Until Event = #PB_Event_CloseWindow
  EndIf
When You click the given both activation buttons, focus is set correctly.
How can I succeed setting the focus after having called the thread by clicking button "do something"?

In my tool I use a thread to do a database query and keep the GUI responsive.

But, how can I set the focus to a specified gadget?

I tried another thread; no way! (I know, from a thread GUI does not update)

Any ideas?


Thanks a lot in advance,
regards!
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PB5.41LTS(x86); Windows7; SetActiveGadget; CreateThread;

Post by bbanelli »

Code: Select all

EnableExplicit

Enumeration #PB_Event_FirstCustomValue
  #Event_ThreadMessage
  #EventType_DisableGadget
  #EventType_SetFocus
EndEnumeration

Procedure OnThreadMessage()
  Select EventType()
    Case #EventType_SetFocus
      SetActiveGadget(EventGadget())
    Case #EventType_DisableGadget
      If EventData()
        DisableGadget(EventGadget(), EventData())
      Else
        DisableGadget(EventGadget(), EventData())
      EndIf
  EndSelect
  ProcedureReturn
EndProcedure

Procedure   doSomething(P.i)
  ;   DisableGadget(5, #True)
  PostEvent(#Event_ThreadMessage, #PB_Ignore, 5, #EventType_DisableGadget, #True)
  Debug "doSomething" : Delay (3000)
  ;   DisableGadget(5, #False)
  PostEvent(#Event_ThreadMessage, #PB_Ignore, 5, #EventType_DisableGadget, #False)
  ;   SetActiveGadget(5) ; this doesn't work
  PostEvent(#Event_ThreadMessage, #PB_Ignore, 5, #EventType_SetFocus, #PB_Ignore)
 EndProcedure
 
 If OpenWindow(0, 0, 0, 270, 240, "SetActiveGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    BindEvent(#Event_ThreadMessage, @OnThreadMessage())
    StringGadget  (0, 10, 10, 250, 20, "bla bla...")
    ComboBoxGadget(1, 10, 40, 250, 21)
    StringGadget  (5, 10, 70, 250, 20, "done")
    
    Define a
    
    For a = 1 To 5 : AddGadgetItem(1, -1, "ComboBox item " + Str(a)) : Next
    SetGadgetState(1, 2)                ; set (beginning with 0) the third item as active one
    ButtonGadget  (2, 10,  100, 250, 20, "Activate StringGadget")
    ButtonGadget  (3, 10, 130, 250, 20, "Activate ComboBox")
    ButtonGadget  (4, 10, 160, 270, 20, "do something")
    Repeat
      Define Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget
        Select EventGadget()
          Case 2 
             SetActiveGadget(0)   ; Activate StringGadget
          Case 3 
             SetActiveGadget(1)   ; Activate ComboBoxGadget
          Case 4
               
             CreateThread(@doSomething(), 0)
                ; SetActiveGadget(5) ; this would work, but I want to await the thread...
        EndSelect
      EndIf
    Until Event = #PB_Event_CloseWindow
  EndIf
HTH,

Bruno
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
HanPBF
Enthusiast
Enthusiast
Posts: 564
Joined: Fri Feb 19, 2010 3:42 am

Re: PB5.41LTS(x86); Windows7; SetActiveGadget; CreateThread;

Post by HanPBF »

Hello Bruno,

thanks a lot for Your help!

So, staying async within events should work.

Great example!
Thanks!
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PB5.41LTS(x86); Windows7; SetActiveGadget; CreateThread;

Post by bbanelli »

HanPBF wrote:Hello Bruno,

thanks a lot for Your help!

So, staying async within events should work.

Great example!
Thanks!
Hi HanPBF,

I hope you've corrected the idiocy I've done in #EventType_DisableGadget. It should really work simply like this:

Code: Select all

    Case #EventType_DisableGadget
      DisableGadget(EventGadget(), EventData())
When Fred introduced bind/post events, it made even GTK and Cocoa as non thread-safe libraries capable of utilizing everything multithreading has to offer.

You can really do wonders with bind/post event functions! Enjoy!

Bruno
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Post Reply