question about adAsyncExecute option

Posted on 2007-09-27
Medium Priority
Last Modified: 2013-12-25
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
Question by:ChenChen
  • 3
  • 3
LVL 14

Accepted Solution

Jai S earned 2000 total points
ID: 19976623
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 ?

Author Comment

ID: 19976652
thankyou for your comment, but I am not familar with multi-threading, please explain. Thanks
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks


Expert Comment

ID: 19976719
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



Expert Comment

ID: 19976721
The GUI might still be responsive if you leave out DoEvents, however, it does no harm in this particular situation.
LVL 14

Expert Comment

by:Jai S
ID: 19976735
KM...i think it will be appropriate to check the links provided before comment "If you are using VB6, you cannot use multithreading." thnx

Expert Comment

ID: 19976744
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.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question