Running programs in the background

  • Thread starter Thread starter timetraveldude
  • Start date Start date
  • Tags Tags
    Programs Running
Click For Summary

Discussion Overview

The discussion revolves around methods for running console programs in the background, particularly on Windows 2000, and how to handle keyboard events regardless of window focus. Participants explore various approaches, including using the Windows API, running applications as services, and utilizing libraries like ncurses.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant asks how to run a console program in the background, similar to DNS or DHCP services.
  • Another participant suggests that specifying the platform would be helpful, later confirming it is Windows 2000.
  • Discussion includes the limitations of the Windows console compared to Unix/Linux, where background processes can be managed with commands like "bg" and "fg".
  • One participant mentions the need for a program to detect keyboard events using the kbhit() function, noting that it only registers input when the window is focused.
  • Another participant proposes using LowLevelKeyboardProc and the Windows API to achieve keyboard detection regardless of focus.
  • There is a suggestion to run applications independently of the console window in Windows, similar to using '&' in Unix/Linux, and the idea of running programs as services is introduced.
  • A participant shares a code snippet using the ncurses library to handle keyboard input, questioning if it would work without the terminal in focus.
  • Another participant asserts that the terminal must be in focus to differentiate input between multiple programs, referencing a prior question about catching input regardless of focus.
  • A workaround is suggested involving setting up a key-event with the desktop to bring the program to the foreground when a specific key is pressed.
  • One participant references a resource on creating a service but expresses uncertainty about a simple method for capturing input not intended for the window.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of capturing keyboard input without window focus, with some asserting it is not possible while others suggest potential workarounds. The discussion remains unresolved regarding the best approach to run programs in the background on Windows.

Contextual Notes

Participants note the complexities involved in using the Windows API and creating services, as well as the limitations of the Windows console compared to Unix/Linux environments.

timetraveldude
Messages
42
Reaction score
0
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
 
Computer science news on Phys.org
It might help to tell us what platform you're talking about.

- Warren
 
chroot said:
It might help to tell us what platform you're talking about.

- Warren
Windows 2000
 
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
 
dduardo said:
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
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.
 
timetraveldude said:
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.
 
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.
 
timetraveldude said:
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.
 
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:

Code:
#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>
 
  • #10
dduardo said:
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?
 
  • #11
Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs.
 
Last edited:
  • #12
dduardo said:
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.
 
  • #13
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.
 
  • #14
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.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
7K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 27 ·
Replies
27
Views
5K
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K