I've observed with c++/non VB apps that when they are doing some CPU intensive work they run quite fast and still respond to user events like mouse-click, keypress etc.
Obviously they are not using any thing like the DoEvents of VB which processes the system message queue. Instead they seem to be giving time to only their own events.
In VB, the only way I know to let your app respond to events is to put a DoEvents in your procedure/loop.
Let's take a very simple example:
VB Code:
Option Explicit
Dim bCancel As Boolean
Private Sub cmdCancel_Click()
If vbYes = MsgBox("Cancel Processing?", vbYesNo + vbQuestion) Then
bCancel = True
End If
End Sub
Private Sub cmdOK_Click()
bCancel = False
Do
If bCancel Then Exit Do
DoSomething
DoEvents
Loop
MsgBox "OK", vbInformation
End Sub
Private Sub DoSomething()
'Add code to do something here
End Sub
This code would work fine, but it is very slow. But if you comment out the Doevents, it would go very fast as it won't process the system message queue. But then it won't respond to cmdCancel_Click and you would ultimately have to End task teh application.
I was looking for some replacement for the DoEvents which won't process the entire system message queue, instead give time to my application only.
Pradeep
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
I was wondering if we could make a dll in another language and implement it in VB applications instead of the DoEvents. I'm not sure how to do this though?
Pradeep
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
I was wondering if we could make a dll in another language and implement it in VB applications instead of the DoEvents. I'm not sure how to do this though?
Pradeep
Now that could be quite possible (although I wouldn't know how to do it either).
Moved back to General VB as requested.
There's still a soft link to this thread in the C/C++ forum.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
This one is faster than the regular DoEvents.
Though it still processes the entire message queue.
Can someone guide me how to identify the messages related to my application only in the message queue?
VB Code:
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private 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
Private Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
Private Const PM_REMOVE = &H1
Dim bCancel As Boolean
'The alternative function for DoEvents:
Private Sub MyDoEvents()
Dim CurrMsg As MSG
'The following loop extract all messages from the queue and dispatch them
'to the appropriate window.
Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
TranslateMessage CurrMsg
DispatchMessage CurrMsg
Loop
End Sub
'uses regular DoEvents
Private Sub Command1_Click()
Dim lCounter As Long, nStart As Double
nStart = Timer
bCancel = False
For lCounter = 1 To 50000
If bCancel Then Debug.Print "Hey! it got Cancelled": Exit Sub
lblCounter.Caption = CStr(lCounter)
DoEvents
Next
Debug.Print "Old Doevents : "; Timer - nStart
End Sub
'uses new MyDoEvents (using API)
Private Sub Command2_Click()
Dim lCounter As Long, nStart As Double
nStart = Timer
bCancel = False
For lCounter = 1 To 50000
If bCancel Then Debug.Print "Hey! it got Cancelled": Exit Sub
lblCounter.Caption = CStr(lCounter)
MyDoEvents
Next
Debug.Print "New Doevents : "; Timer - nStart
End Sub
'Signals a cancel
Private Sub Command3_Click()
Debug.Print "I clicked Cancel"
bCancel = True
End Sub
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
I havn't tried this, although it looks interesting, however I believe you should be able to simply specify your hwnd. i.e, change
VB Code:
Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
to
VB Code:
Do While PeekMessage(CurrMsg, Me.hwnd, 0, 0, PM_REMOVE) <> 0
That increases the time taken
Pradeep
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering