Reputation: 1662
I work on a very old VB6 application which is still our companies main project. I had never worked in Visual Basic much less VB6 before starting on this project. I have run into alot of places that have doevents to keep the form responsive and the more I dig into what doevents
does the more I have to scratch my head when attempting to debug code litered with them.
I am working on a way to possibly tell whether a doevent is actually going to do anything when we hit that line of code. I was hoping if some one who has more knowledge of window pumps and the messaging queue could tell me whether my approach will be effective in telling me whether this approach will work. I am attempting to use PeekMessage to see if their are any messages in the queue right before I hit a doevents line.
Public Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Type msg
hWnd As Long
Message As Long
Wparam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Sub PeekAtMessages()
Dim oPeekMessage As msg
Dim bPeekMessage As Boolean
Dim lCtr As Long
bPeekMessage = True
bPeekMessage = PeekMessage(oPeekMessage, frmMain.hWnd, 0, 0, 0)
Debug.Assert bPeekMessage = True
Debug.Print "DoEvents: Message= Hex(" & Hex(oPeekMessage.Message) & ") Dec(" & oPeekMessage.Message & ") hWnd=" & oPeekMessage.hWnd & " lParam=" & oPeekMessage.lParam & " time=" & oPeekMessage.time & " Wparam=" & oPeekMessage.Wparam
End Sub
Public Sub MyDoEvents()
PeekAtMessages
DoEvents
End Sub
So I was hoping that when I call MyDoEvents
I would only hit the Debug.Assert bPeekMessage = True
if no messages were in the queue. This would allow me when working through old uncommented code to tell whether a doevents actually did something.
I tested this out and it looked like I only hit the Debug.Assert
when there are no events but I still don't know if my findings are acurate/reliable. Has anyone else had experience with attempting to view what at doevent is about to do?
Upvotes: 2
Views: 1204
Reputation: 86
The thing to remember is that DoEvents is a magical word. People who have vague or no idea what it does randomly try it in case it works.
VB6 clears it's own message queue, then calls Sleep(0), which means all other apps get to clear their queues.
Of course VBRuntime interupting your code and executing VB6 events has the same problems that multithreading have, global/static variables can be accessed, partially updated, etc. I've never seen synchronisation around a DoEvents statement or that any thought has been given to it.
Spy++ window message spyer is included with VB6 and SDK.
In Windows some messages are generated on demand. Timers, Paint, and a few others aren't actually in the queue. If the queue is empty and the program queries then Windows looks to see if the paint or other flags are set. If so, generates a message and clears that flag. So Paint etc are FLAGS not messages in a queue.
Upvotes: 1