PostEvent() help, help

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

PostEvent() help, help

Post 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
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PostEvent() help, help

Post 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
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: PostEvent() help, help

Post 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...
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PostEvent() help, help

Post 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
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: PostEvent() help, help

Post 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!
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PostEvent() help, help

Post 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
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
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PostEvent() help, help

Post by Caronte3D »

Thanks mk-soft and SMaag :D
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: PostEvent() help, help

Post 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
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PostEvent() help, help

Post by Caronte3D »

You better use #PB_Event_FirstCustomValue as mk-soft pointed :wink:
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: PostEvent() help, help

Post by jak64 »

OK thanks
Post Reply