Solved

GetAsyncKeyState consuming almost all CPU

Posted on 2007-03-20
5
326 Views
Last Modified: 2013-12-04
Hi all,

I am currently using GetAsyncKeyState to launch an application. It works when the user presses a combination of keys, then creates the target application using CreateProcess. However, it seems that my PC is beginning to be sluggish since I had that app running. It loads from start up via the registry key

HKLM/Software/Microsoft/Current Version/Run (can't remember if the path is right, but you get what I mean)

The program is windowless and runs in a loop checking for the correct key combinations entered by the user.

A snippet of the code is as follows.

while(1)
{
....................................
if(GetAsyncKeyState(VK_CONTROL))             // Ctrl + Alt + G
                if(GetAsyncKeyState(VK_MENU))
                        if(GetAsyncKeyState('G'))
                                     launchApp();
....................................
}

In launchApp

launchApp()
{
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

        CreateProcess( NULL, "pxapp.exe", NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
}

I guess is that loop while(1) that is consuming all the resources, so it checks for key every few miliseconds, not elegant. Is there any way of optimising this program? Perhaps put it in idle state and trigger an event when received those key combinations. Will it help if I include a Sleep in the loop? Any ideas?

Thanx.
0
Comment
Question by:fart_boy21
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 200 total points
ID: 18762943
Simply put a sleep ( 10 ) or similar to release the CPU:


while ( 1 )
{
   if(GetAsyncKeyState(VK_CONTROL))             // Ctrl + Alt + G
                if(GetAsyncKeyState(VK_MENU))
                        if(GetAsyncKeyState('G'))
                                     launchApp();
   Sleep ( 10 );  
}

HTH
0
 

Author Comment

by:fart_boy21
ID: 18767454
Oh. Thats what I thought. But then will it not miss some keystrokes since the app is "sleeping"?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18769359
If your Sleep() interval is too long then, yes, you may miss a keystroke here and there.  You could probably go up to 50 though without losing anything...

An alternative would be to use a low level keyboard hook via WH_KEYBOARD_LL.
0
 

Author Comment

by:fart_boy21
ID: 18784447
Ok thanks all for the answers. The Sleep really helped. I am just a beginner in Windows programming. I do not know the architechure of it.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 18806748
Glad you got it sorted.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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