Solved

GetAsyncKeyState consuming almost all CPU

Posted on 2007-03-20
5
322 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now