Page 1 of 1

PostEvent() help, help

Posted: Sun Mar 02, 2025 2:01 pm
by jak64
Hello everyone,
This is my first time using PostEvent and there is something I don't understand.

In the small test program below, when I launch the program in the PureBasic editor, the message appears after 2 seconds (the 2 seconds simulate the duration of the procedure).

On the other hand, if I compile the program and launch the .exe, the message never appears!

I searched the documentation but couldn't find why!

Thank you for your help

ps: I did not put a button to launch the procedure because the program that I write with PostEvent will be launched by another program and must start automatically.

Code: Select all

EnableExplicit

Declare traitement()

Global fenetre.i
Global bt_quitter
Global traitement.i
Global gadgetid.i
Global txt_message.i
Global quitter.b


fenetre = OpenWindow(#PB_Any, 0, 0, 400, 200, "PostEvent", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
bt_quitter = ButtonGadget(#PB_Any, 290, 160, 100, 30, "Quitter")
txt_message =TextGadget(#PB_Any, 10, 100, 200, 30, "")

quitter = #False

traitement()

Repeat
  
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      quitter = #True
      
      ;===== Evènements sur les gadgets
    Case #PB_Event_Gadget
      gadgetid =  EventGadget()
      
      ;===== Clic sur bouton Quitter
      If gadgetid = bt_quitter
        Select EventType()
          Case #PB_EventType_LeftClick
            quitter = #True
        EndSelect 
      EndIf
      
      ;===== PostEvent
      Case  traitement
        SetGadgetText(txt_message, "Traitement terminé")

  EndSelect        
  
Until quitter
CloseWindow(fenetre)
End

Procedure traitement()
  Delay(2000) ; simulate procedure duration
  PostEvent(traitement)
EndProcedure

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 2:22 pm
by Caronte3D

Code: Select all

EnableExplicit


Enumeration #PB_EventType_FirstCustomValue
  #MyEvent  
EndEnumeration


Declare traitement()

Global fenetre.i
Global bt_quitter
Global gadgetid.i
Global txt_message.i
Global quitter.b


fenetre = OpenWindow(#PB_Any, 0, 0, 400, 200, "PostEvent", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
bt_quitter = ButtonGadget(#PB_Any, 290, 160, 100, 30, "Quitter")
txt_message =TextGadget(#PB_Any, 10, 100, 200, 30, "")

quitter = #False

traitement()

Repeat
  
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      quitter = #True
      
      ;===== Evènements sur les gadgets
    Case #PB_Event_Gadget
      gadgetid =  EventGadget()
      
      ;===== Clic sur bouton Quitter
      If gadgetid = bt_quitter
        Select EventType()
          Case #PB_EventType_LeftClick
            quitter = #True
        EndSelect 
      EndIf
      
      ;===== PostEvent
      Case  #MyEvent
        SetGadgetText(txt_message, "Traitement terminé")

  EndSelect        
  
Until quitter
CloseWindow(fenetre)
End

Procedure traitement()
  Delay(2000) ; simulate procedure duration
  PostEvent(#MyEvent)
EndProcedure

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 2:44 pm
by jak64
Hello Caronte3D,

Thank you for your response, it works correctly now.

I don't think I would have found the solution on my own...

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 2:52 pm
by Caronte3D
The most of the solutions exists already on this awesome forum, so... it's a good routine to read periodically every single post, because you can learn a lot from :D

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 4:17 pm
by SMaag
You post Event with value 0. Not good!



for user events you have to use

Global traitement.i = #PB_EventType_FirstCustomValue

Then it works!

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 4:54 pm
by mk-soft
Small bug by Caronte3D ;) probably mistyped
For Custom Event

Code: Select all

Enumeration #PB_Event_FirstCustomValue
  #MyEvent
EndEnumeration
For Custom Gadget Event Type

Code: Select all

Enumeration #PB_EventType_FirstCustomValue
  #MyEventType
EndEnumeration

Re: PostEvent() help, help

Posted: Sun Mar 02, 2025 5:38 pm
by Caronte3D
Thanks mk-soft and SMaag :D

Re: PostEvent() help, help

Posted: Mon Mar 03, 2025 9:28 am
by jak64
Hello Caronte3D, SMaag and mk-soft,

I don't understand your messages.

Does the following small program need to be modified?

Thanks to you

Code: Select all

EnableExplicit


Enumeration #PB_EventType_FirstCustomValue
  #MyEvent  
EndEnumeration


Declare traitement()

Global fenetre.i
Global bt_quitter
Global gadgetid.i
Global txt_message.i
Global quitter.b


fenetre = OpenWindow(#PB_Any, 0, 0, 400, 200, "PostEvent", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
bt_quitter = ButtonGadget(#PB_Any, 290, 160, 100, 30, "Quitter")
txt_message =TextGadget(#PB_Any, 10, 100, 200, 30, "")

quitter = #False

traitement()

Repeat
  
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      quitter = #True
      
      ;===== Evènements sur les gadgets
    Case #PB_Event_Gadget
      gadgetid =  EventGadget()
      
      ;===== Clic sur bouton Quitter
      If gadgetid = bt_quitter
        Select EventType()
          Case #PB_EventType_LeftClick
            quitter = #True
        EndSelect 
      EndIf
      
      ;===== PostEvent
      Case  #MyEvent
        SetGadgetText(txt_message, "Traitement terminé")

  EndSelect        
  
Until quitter
CloseWindow(fenetre)
End

Procedure traitement()
  Delay(2000) ; simulate procedure duration
  PostEvent(#MyEvent)
EndProcedure

Re: PostEvent() help, help

Posted: Mon Mar 03, 2025 1:07 pm
by Caronte3D
You better use #PB_Event_FirstCustomValue as mk-soft pointed :wink:

Re: PostEvent() help, help

Posted: Mon Mar 03, 2025 1:45 pm
by jak64
OK thanks