question about adAsyncExecute option

When you run a query in the Query Analyzer from a client PC, all work will be done on the server,  If you open the task manager you will see that the usage of client's CPU hardly get increased at all. but at the same time the GUI in client PC still keeps responding, there is even a moving timer.
However, I tried to execute the same query in my VB program (running on the same PC) using command object. the problem I have is if I use adAsyncExecute option, and keep a while loop to check command.state = adStateExecuting, the VB GUI will be responding but CPU usage will be up to about 50%; and If I don't use the option, CPU usage won't increase but it freezes up the GUI.

Does anyone know how to keep CPU usages low and program responding at the same time, like the Query Analyzer does?

Thanks in advance
Who is Participating?
Jai SConnect With a Mentor Tech ArchCommented:
if you put a normal WHILE loop - an empty one...the processro will keep increasing even though the loop does not do anything...tht is the architecture of how VB works..and the CPU will increase no mattter the loop does anything or not...
is it possible for you to start a thread on the DB operation and wait until the thread returns true ?
ChenChenAuthor Commented:
thankyou for your comment, but I am not familar with multi-threading, please explain. Thanks
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

If you are using VB6, you cannot use multithreading. Use a While loop that calls DoEvents/Sleep, and you'll reduce CPU usage while maintaining responsiveness of your app:

Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Sub ExecuteSomeQuery()
   myCommand.Execute ..., adAsyncExecute
   Do While myCommand.State = adStateExecuting
      DoEvents ' this will ensure that your app is responsive
      Sleep 1 ' this will put your process asleep for 1 millisecond or so
End Sub

My example does not cover issues like error handling. Remember that you should, as a rule of thumb, disable all of your GUI except of your "cancel" button while you are in such a loop.

Note that this approach works for VB.NET, too, however, using threads is somewhat more elegant. Also, you might want to substitute the call to Sleep with something like


The GUI might still be responsive if you leave out DoEvents, however, it does no harm in this particular situation.
Jai STech ArchCommented:
KM...i think it will be appropriate to check the links provided before comment "If you are using VB6, you cannot use multithreading." thnx
Well, messing with CreateThread in VB6 is somewhat unsupported, good luck to you :-) I've tried that ages ago, it didn't work out for me for reasons I don't know anymore.

Waiting in a sleeping loop is not as elegant as using a worker thread, but it's guaranteed to work on VB6, and it might be simpler, too.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.