Page 1 of 1
Waitwindowevent - catch user messages?
Posted: Sun Apr 01, 2018 10:19 pm
by Osmdesat
Is it possible to catch user window messages using WaitWindowEvent function, i.e. messages with uMsg >= WM_USER posted to my window? In the list of possible events is no such kind of event.
Re: Waitwindowevent - catch user messages?
Posted: Sun Apr 01, 2018 10:25 pm
by RSBasic
This is possible with SetWindowCallback(), if that's what you mean.
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 4:22 am
by TI-994A
Osmdesat wrote:Is it possible to catch user window messages using WaitWindowEvent function, i.e. messages with uMsg >= WM_USER posted to my window?
As RSBasic had pointed out, you'd have to implement a callback to intercept messages that are not supported by PureBasic natively; and that too would provide only an OS-specific solution. However, you might also consider using PureBasic's very own
PostEvent() function, which is cross-platform:
https://www.purebasic.com/documentation ... event.html
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:05 am
by Osmdesat
Thanks guys. Custom callback is what I wanted to avoid - that's why I asked my question. But PostEvent() with custom Event will work pretty well for me (:
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:18 am
by Dude
Why is a callback needed anyway? It just seems weird that we have to have two separate sections of code to process all event messages. Is there some technical reason, or is it simply to keep the cross-platform messages separate from Windows-specific messages? I assume the answer is the latter reason, because PureBasic used to accept all Windows-specific messages in the past from WaitWindowEvent().
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:33 am
by mk-soft
It is only possible in callback to the message with another return value
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:42 am
by Osmdesat
@Dude
Callback is not needed. I wrote I wanted to avoid it, if possible. I totally agree that it would be nonconsistent to have callback and WaitWindowEvent section.
My aim is to post a custom message from a thread to the main message loop, so that program knows that the thread has finished. I found no better way how to do it in non-blocking manner.
But PostEvent() function solves it without need of callback - there is even a thread message example there in documentation.
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:50 am
by Dude
Osmdesat wrote:PostEvent() function solves it without need of callback
Maybe I'm misunderstanding, but how can PostEvent() let my app know that a #WM_NOTIFY message has occurred? AFAIK, only a callback can detect that sort of thing.
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 11:11 am
by mk-soft
Only over SetWindowCallback...
Here find some examples
Google 'WM_NOTIFY site:purebasic.fr'
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 8:10 pm
by Osmdesat
Why WM_NOTIFY? Originally I wanted to send WM_USER using PostMessage_ function.
But if it's possible to send #PB_Event_FirstCustomValue using PostEvent() function and WaitWindowEvent() will catch it, I can do it this way.
Re: Waitwindowevent - catch user messages?
Posted: Mon Apr 02, 2018 10:33 pm
by mk-soft
Sorry,
I had misunderstood...
Small trick with SendEvent...
Code: Select all
;-TOP
; ***************************************************************************************
;-Begin Of SendEvent
; Comment : SendEvent
; Author : mk-soft
; Version : v1.06
; Create : unknown
; Update : 07.08.2016
;- Structure
Structure udtSendEvent
Signal.i
Result.i
*pData
EndStructure
; ---------------------------------------------------------------------------------------
Procedure SendEvent(Event, Window = 0, Object = 0, EventType = 0, pData = 0, Semaphore = 0)
Protected MyEvent.udtSendEvent, result
With MyEvent
If Semaphore
\Signal = Semaphore
Else
\Signal = CreateSemaphore()
EndIf
\pData = pData
PostEvent(Event, Window, Object, EventType, @MyEvent)
WaitSemaphore(\Signal)
result = \Result
If Semaphore = 0
FreeSemaphore(\Signal)
EndIf
EndWith
ProcedureReturn result
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure SendEventData(*MyEvent.udtSendEvent)
ProcedureReturn *MyEvent\pData
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure DispatchEvent(*MyEvent.udtSendEvent, result)
*MyEvent\Result = result
SignalSemaphore(*MyEvent\Signal)
EndProcedure
;- End Of SendEvent
; ***************************************************************************************
;- Example
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
; ***************************************************************************************
;- Memory string helper
Procedure AllocateString(String.s)
Protected *mem.string
*mem = AllocateStructure(string)
If *mem
*mem\s = String
EndIf
ProcedureReturn *mem
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure.s FreeString(*mem.string)
Protected result.s
If *mem
result = *mem\s
FreeStructure(*mem)
EndIf
ProcedureReturn result
EndProcedure
; ***************************************************************************************
Enumeration
#Window
EndEnumeration
;- Constants
Enumeration #PB_Event_FirstCustomValue
#My_Event_Messagebox
#My_Event_Inputbox
EndEnumeration
Procedure Thread1(Null)
Debug "Init Thread 1"
Protected result
Repeat
Delay(500)
result = SendEvent(#My_Event_Messagebox, 0, 0, 0, Random(100))
Select result
Case #PB_MessageRequester_Yes
Debug "#PB_MessageRequester_Yes"
Case #PB_MessageRequester_No
Debug "#PB_MessageRequester_No"
Case #PB_MessageRequester_Cancel
Debug "#PB_MessageRequester_Cancel"
EndSelect
Until result = #PB_MessageRequester_Cancel
Debug "Exit Thread 1"
EndProcedure
Procedure Thread2(Null)
Debug "Init Thread 2"
Protected result, text.s
Repeat
Delay(500)
result = SendEvent(#My_Event_Inputbox, 0, 0, 0, Random(100))
text = FreeString(result)
Debug text
Until text = ""
Debug "Exit Thread 2"
EndProcedure
Procedure Main()
Protected exit, th1, th2
Protected MyEvent, value, result, text.s
If OpenWindow(#Window, 0, 0, 800, 600, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
th1 = CreateThread(@Thread1(), #Null)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Case #My_Event_Messagebox
MyEvent = EventData()
value = SendEventData(MyEvent)
result = MessageRequester("Thread 1", "What happens next?" + #LF$ + "(Code " + value + ")", #PB_MessageRequester_YesNoCancel)
DispatchEvent(MyEvent, result)
If result = #PB_MessageRequester_Cancel
th2 = CreateThread(@Thread2(), #Null)
EndIf
Case #My_Event_Inputbox
MyEvent = EventData()
text = InputRequester("Thread 2", "Name:", "")
result = AllocateString(text)
DispatchEvent(MyEvent, result)
EndSelect
ForEver
If IsThread(th1)
KillThread(th1)
EndIf
If IsThread(th2)
KillThread(th2)
EndIf
EndIf
EndProcedure : Main()
CompilerEndIf
Re: Waitwindowevent - catch user messages?
Posted: Tue Apr 03, 2018 11:11 am
by Osmdesat
Thanks mk-soft.