View Full Version : Running programs in the background
timetraveldude
May22-04, 12:12 PM
does anyone know how to run a console program in the background. This would be like the DNS or DHCP service that runs on a server waiting for a request.
thanks
It might help to tell us what platform you're talking about.
- Warren
timetraveldude
May22-04, 04:58 PM
It might help to tell us what platform you're talking about.
- Warren
Windows 2000
dduardo
May22-04, 06:12 PM
Good luck. Microsoft's console isn't the most robust thing in the world.
Unix or Linux:
ctrl-z the program in the console. It will display a number along with the name of the program. Then you can do "bg [number]" or "fg [number]" depending if you want the program in the background or foreground
timetraveldude
May22-04, 06:26 PM
Good luck. Microsoft's console isn't the most robust thing in the world.
Unix or Linux:
ctrl-z the program in the console. It will display a number along with the name of the program. Then you can do "bg [number]" or "fg [number]" depending if you want the program in the background or foregroundWhat I want to do is have a program wait for a keyboard event using the kbhit() function. The problem I am having is that the program will only register a keyboard event if the window the program started from is clicked upon. What I need to do is have the program detect a keyboard press reagardless of whether the windows running the program is in focus.
I am running this through the DOS console on a windows 2000 machine.
master_coda
May22-04, 07:26 PM
What I want to do is have a program wait for a keyboard event using the kbhit() function. The problem I am having is that the program will only register a keyboard event if the window the program started from is clicked upon. What I need to do is have the program detect a keyboard press reagardless of whether the windows running the program is in focus.
I am running this through the DOS console on a windows 2000 machine.
The only way I know of to do that is with a LowLevelKeyboardProc. You would need to use the Windows API, though.
timetraveldude
May22-04, 09:05 PM
What about just running an application independently of the console window. For example, under unix or linux you can type 'my_prog &' with the '&' sign. If you close the window the program was launched from the program will still run. Is there a way to do this under windows.
thanks for all the help.
master_coda
May22-04, 10:21 PM
What about just running an application independently of the console window. For example, under unix or linux you can type 'my_prog &' with the '&' sign. If you close the window the program was launched from the program will still run. Is there a way to do this under windows.
thanks for all the help.
If you want an application to run in the background, you can run it as a service. That can be rather difficult to do, since writing a service involves some rather arcane API calls (at least it used to...maybe things have changed). But that is the standard way in Windows to have programs run in the background.
I believe there are tools that will let you run an ordinary console or window app as a service. That's probably a lot easier.
dduardo
May23-04, 08:19 AM
Here is what I suggest. Get cygwin from here: http://www.cygwin.com/
Make sure to install g++. write a program using the ncurses library. Here is some sample code to take ctrl-c, ctrl-d, etc as characters and you can process them as you like:
#include <ncurses.h>
int main( void )
{
int c ;
initscr() ; // Intialize Screen
raw() ; // Disable Line Buffering
keypad(stdscr, TRUE) ; //Read Function Keys and Arrows
noecho() ; // Disable printing input to screen without permission
printw("Type any character\n") ; // Equivalent to printf
c = getch() ; // Wait for character input
printw("%c was pressed",c) ; // Equivalent to printf
refresh() ; // Print information from stdscr to visible window
getch() ; // Wait for character input
endwin() ; // Destroy Screen
return 0 ;
}
When you compile this piece of code with g++ make sure to do it this way:
g++ -lncurses <filename.cpp>
master_coda
May23-04, 09:40 AM
Here is what I suggest. Get cygwin from here: http://www.cygwin.com/
Make sure to install g++. write a program using the ncurses library. Here is some sample code to take ctrl-c, ctrl-d, etc as characters and you can process them as you like:
When you compile this piece of code with g++ make sure to do it this way:
g++ -lncurses <filename.cpp>
Will that work when the terminal doesn't have focus?
dduardo
May23-04, 09:44 AM
Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs.
master_coda
May23-04, 09:54 AM
Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs
timetraveldude asked if there was a way to catch all input regardless of whether or not his program was in focus.
dduardo
May23-04, 10:29 AM
Here is workaround hack:
Setup a key-event with your desktop. This is going to differ across desktop environments. When a specific key is pressed, have it run a script that checks to see if your programming is running. If it is, bring the program to the foreground.
Dissident Dan
May24-04, 12:35 PM
How to make a service:
http://www.codeguru.com/Csharp/.NET/cpp_managed/windowsservices/article.php/c4877/
----------
I'm pretty sure that there is some simple way to get input that wasn't intended for your window, but I forget what it is.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.