Advertisement

Getting a program to run in the background

Started by February 23, 2001 05:32 PM
2 comments, last by slayemin 23 years, 11 months ago
I''m trying to write a program which waits for the user to punch in a hotkey, and run in the background of windows. However, my program runs in a while(1) loop which hogs up the CPU. I can''t figure out a way to make the program look for a key-press and keep going if it doesn''t find one. Heres the pseudo-code pretty much for what I''m doing: winMAIN(blah) while(1) if(keypressed 1 is true) Do task1 if(keypressed 2 happens) Do task2 if(keypressed 3 happens) Do task3 ... The program runs fine, but when it runs, it takes up 99% of the CPU time (because of the while loop). Is there any way to make the program keep running without the while loop?
I''m not quite sure but I think you are trying to run the program and keep a separate loop going at the same time to check for keyboard input, right? Have you tried creating two processes. If you spawn two processes, one can run the main program and the other can be a low priority process that waits for keyboard input. In UNIX I know you use fork() to create two processes. I think in Win32 you might use spawn or one of its related functions.
Advertisement
Why don''t you just tag it on some of those message queues windows has and wait there for messages? Assuming you can somehow get the "global keystate" somewhere from those messages.


spawn* is just a way to run a program. It creates a new process yes, but that''s exactly where the newly run program goes. spawn* is the same as fork() and then exec*() on unix. It is however not POSIX and should be avoided at all cost . I do believe there is a native CreateProcess (or such) function in the WinAPI.


"This album was written, recorded and edited at Gröndal, Stockholm in the year of 2000. At this point in time money still ruled the world. Capitalistic thoughts were wide spread. From the sky filled with the fumes of a billionarie''s cigar to the deepest abyss drenched in nuclear waste. A rich kid was a happy kid, oh..dirty, filthy times. Let this be a reminder."
- Fireside, taken from back of the Elite album


if u don''t want it to hog ur cpu, just add
sleep(2);
or something like that. if you''re program goes thru that loop like 200 times a second, then sleep(2) would be enuf. if ur framerate is lower, try a higher number, like sleep(20);


thuned

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)

This topic is closed to new replies.

Advertisement