Priority of custom events / PostEvent()

Just starting out? Need help? Post your questions and find answers here.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Priority of custom events / PostEvent()

Post by #NULL »

PB570b1, Ubuntu 16.04.
If I post a custom event then it seems to have priority over other events and I wonder if this is by design or something wrong with my code or maybe a PB bug? For example if I disable a button and then post a custom event, the disable will not be visible until custom events have been processed.
With the code below the buttons will disable each other when clicked, but when you comment-in the line marked with [A] then the search will generate continuous search events until finished and the disabling of the search button won't become visible until the end of the search and the button is then finally enabled again. Also the button 'hangs' during the search (even more visible with gtk2).
I tried also PostEvent(#PB_Event_Gadget, win, progress, #eventTypeSearch), i.e. using a custom event type and posting it to an event/window/object, but with the same result.
If using a thread it may work just by luck because the event queue might be emptied between the PostEvent()s, but I did not try that.

Code: Select all

EnableExplicit

Enumeration #PB_Event_FirstCustomValue
  #eventSearch
EndEnumeration

Define win, progress, bSearch, bCancel
Define quit

Define searching = #False
Define search = 0

Procedure search()
  Shared win
  Shared searching, search
  Shared bSearch, bCancel
 
  If searching
    search + 1
    Debug "searching " + search + " .."
    Delay(100)
    If search >= 10
      Debug "finished"
      searching = #False
    Else
      PostEvent(#eventSearch)
    EndIf
  Else
    Debug "stop"
  EndIf
 
  If Not searching
    search = 0
    DisableGadget(bCancel, 1)
    DisableGadget(bSearch, 0)
  EndIf
EndProcedure

win = OpenWindow(#PB_Any, 100, 100, 420, 130,"window", #PB_Window_ScreenCentered)
AddKeyboardShortcut(win, #PB_Shortcut_Escape, 99)
progress = ProgressBarGadget(#PB_Any, 10, 10, 400, 30,  0, 100)
bSearch = ButtonGadget(#PB_Any, 10, 50, 400, 30, "search")
bCancel = ButtonGadget(#PB_Any, 10, 90, 400, 30, "cancel")
DisableGadget(bCancel, 1)

Repeat
  WaitWindowEvent()
  If EventWindow() = win
    If Event() = #PB_Event_Menu And EventMenu() = 99
      quit = #True
    ElseIf Event() = #PB_Event_CloseWindow
      quit = #True
    ElseIf Event() = #PB_Event_Gadget
      If EventGadget() = bSearch
        DisableGadget(bSearch, 1) ; (won't be visible)
        DisableGadget(bCancel, 0) ; (won't be visible)
        searching = #True
        ;PostEvent(#eventSearch) ; [A]
      ElseIf EventGadget() = bCancel
        DisableGadget(bCancel, 1)
        DisableGadget(bSearch, 0)
        searching = #False
        PostEvent(#eventSearch)
      EndIf
    EndIf
  ElseIf Event() = #eventSearch ; (posted without a window, that's why it's here in the else)
    Debug "search event"
    search()
  EndIf
Until quit
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Priority of custom events / PostEvent()

Post by mk-soft »

I don't know if it's a bug.
But it's the same with windows. It seems to be true that the post event buffer is processed first.
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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Priority of custom events / PostEvent()

Post by #NULL »

Thanks for having a look.
It seems to depend on the events. If PB events are being generated like #PB_EventType_Focus then at least they are reported in the correct/expected order:

Code: Select all

win = OpenWindow(#PB_Any, 50,100, 800, 600, "..")
st = StringGadget(#PB_Any, 10, 10, 100, 20, "st")

PostEvent(#PB_Event_FirstCustomValue)
SetActiveGadget(st)
SetActiveGadget(-1)
PostEvent(#PB_Event_FirstCustomValue)
SetActiveGadget(st)
SetActiveGadget(-1)

Repeat
  WaitWindowEvent()
  Select Event()
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_Focus : Debug "got focus"
        Case #PB_EventType_LostFocus : Debug "lost focus"
      EndSelect
    Case #PB_Event_FirstCustomValue : Debug "custom"
  EndSelect
Until Event() = #PB_Event_CloseWindow

Code: Select all

custom
got focus
lost focus
custom
got focus
lost focus
Post Reply