Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DoEvents Overhead 1

Status
Not open for further replies.

Error7

Programmer
Jul 5, 2002
656
GB
I have been experimenting with DoEvents to allow the user to cancel processing of the following code.

Without using DoEvents the process takes 1 min 27 sec.
With DoEvents in the outer loop as shown, it takes 3 min 1 sec.

But if I Rem out the first DoEvents and un-rem the second the process takes a whopping 8 min 45 secs.

For a = 0 To cmbSites.ListCount - 1
DoEvents
If CancelErr Then Exit For
Open App.Path & "\" & IP(a) & ".txt" For Output As #2
For i = 0 To lstCsvfiles.ListCount - 1
StatusBar1.Panels(1).Text = "Scanning " & lstCsvfiles.List(i) & " for " & IP(a)
Open lstCsvfiles.List(i) For Input As #1
Do While Not EOF(1)
'DoEvents
'If CancelErr Then Exit For
Line Input #1, FileString
If InStr(FileString, IP(a)) Then
Print #2, FileString
End If
Loop
Close #1
Next i
Close #2
Next a

Is there a more efficient way of allowing the user to cancel a process?

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
>a whopping 8 min 45 secs

And this is just one of the reasons that you will find a number of us in here advising against the use of DoEvents if it can be avoided ...
 
I seem to recall that there is (was??) an API that can be used instead which is less intrusive. For the life of me I can't remember it though.

I'm sure there's many here who will point you in the right direction.

Patrick
 
Only use DoEvents every x amount of records.

Dim x as Long

For x = 0 To 5000000
If x Mod 1500 Then DoEvents
If CancelErr Then Exit For
Next

Swi
 
Thanks Patrick.

I would like to include a cancel option at some point in the loops because the times I provided earlier are from a partial run of the process.

A complete run is likely to take some considerable time even without DoEvents.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks Swi

Effectively that is what I am doing here:

For a = 0 To cmbSites.ListCount - 1
DoEvents

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Oh, I wouldn't necessarily go with GetInputState either.

I was going to post a suggestion that you consider a PeekMessage/TranslateMessage/DispatchMessage solution - but see that the thread redsmooth provides covers this
 
I didn't test the code that was there but why wouldn't you recommend GetInputState??
 
OK - what would you do if GetInputState returned a nonzero value?

i.e how would you deal with there being a pending message in the moue/keyboard input queue?
 
How about using GetAsyncKeyState instead? This doesn't read the buffer, only if the key is currently being held down. Use a function key and check to see if it is down. If it is, exit the routine.

Robert
 
Thank you Robert.

GetAsyncKeyState was easy to implement and doesn't appear to have any measureable overhead.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
>GetAsyncKeyState

One drawback of the above:

Given that the process takes, by your reckoning, 1 min 27 seconds to run it is entirely possible that the user might switch to another application. So what happens then?
 
Thanks strongm, and thanks for your earlier input.

I was aware that this might be a problem in general practice but it is unlikely to occur in this particular application.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
And if you were concerned about it occuring, you could put a test in your code to see if your application had focus or not. If it doesn't have focus, it would ignore ( or not even check ) the GetAsyncKeyState.

Robert
 
Thanks Robert, I will add that to my app.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top