I've used this snippet in some VB programs and found it very
useful when you need for your program to sleep without decreasing
the performance of the CPU.

Basically i would like to see an example with the WaitForMultipleObjects API
for an application without hWnd.

I've looked at one of Iczelion's tutorials on threads where he used the WaitForSingle.. API, but I can't
figure it out.

Here's the VB code to give you and idea of what i want to do.

Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long

Public Sub SafeSleep(ByVal inWaitSeconds As Single)

Const WAIT_OBJECT_0 As Long = 0
Const WAIT_TIMEOUT As Long = &H102

Dim lastTick As Single
Dim timeout As Long
timeout = inWaitSeconds * 1000
lastTick = Timer

Select Case MsgWaitForMultipleObjects(0, 0, False, timeout, 255)
timeout = ((inWaitSeconds) - (Timer - lastTick)) * 1000
If timeout < 0 Then timeout = 0

Case Else
Exit Do

End Select

Loop While True

End Sub

Thanks in advance!
Posted on 2003-03-14 06:26:13 by pub^
here's what I have used for a message/event loop in a server

It seems to be a good idea to enter the loop with PeekMessage. For some reason, the very first message a
program gets doesn't always seem to trigger the MsgWaitForMultipleObjects...

Logic is basically as follows:
1. Wait for a message or event.
2. If it's a message, go and remove it with PeekMessage and Dispatch it. Or exit if WM_QUIT
3. If it's an event, handle the event
4. Either way go back to 1


@@GetMessages: invoke PeekMessage,addr msg,NULL,NULL,NULL,PM_REMOVE
test eax,eax
jz @@MessageLoop
cmp msg.message,WM_QUIT
je @@Exit
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
@@MessageLoop: invoke MsgWaitForMultipleObjects,1,addr hEvent,FALSE,INFINITE,QS_ALLINPUT
test eax,eax
jnz @@GetMessages
@@Event: ... ;Do whatever it is you want your event to do
jmp @@MessageLoop
@@Exit: ... ;Clean up your proggy
invoke ExitProcess,msg.wParam
Posted on 2003-03-14 16:44:04 by chorus
I saw similar idea in buliaNaza's post and if you manage more server events you can
periodically change places of their handles before calling MsgWaitForMultipleObject API

hEvent1, hEvent2, hEvent3...... hEventN-1, hEventN

hEventN, hEvent1, hEvent2, hEvent3...... hEventN-1

hEventN-1, hEventN, hEvent1, hEvent2, hEvent3.....
hEvent1, hEvent2, hEvent3...... hEventN-1, hEventN

Posted on 2003-03-14 20:13:47 by lingo12
Perfect :alright:

Posted on 2003-03-30 10:58:19 by pub^