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