File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to keep a program running until the user terminates it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to keep a program running until the user terminates it?" Watch "How to keep a program running until the user terminates it?" New topic
Author

How to keep a program running until the user terminates it?

Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Hi all, I'm sorry if my question has been already asked, but I tried to find out any related thread, but couldn't find!

Anyways, I'm working on a program, that prints out some results, and keeps printing until the user terminates the program.

My question is how can I let the user terminate the running program?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11302
    
  16

how do you want them to terminate it?

The simplest solution, although not very elegant, would be to have an infinite loop, and they have to ctrl-c out:

while (true)
{
//your code here
}

of course, that immediately kills the process, with no polite way to clean up, but it works

slightly more complicated, you could put in a keyboard listener. have the program loop until the user hits a specific key - this would let you break out of a loop and do any cleanup


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
Better solution is to have an entry like "stop" which somebody else is enquiring about on this very forum this very day.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:Better solution is to have an entry like "stop" which somebody else is enquiring about on this very forum this very day.


Thanks for replying..

The "stop" entry doesn't suit my program. I think!

Because the user will just be watching the outputs, and the process is not waiting for an input from him/her ...

Am I right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
In which case, Fred's suggestions about keyboard listeners might be better. How do you add a keyboard listener to a command-line application, Fred?
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
come on fred, how is it done?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
Abdulmalik Malik wrote:come on fred, how is it done?
Patience Is A Virtue
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:
Abdulmalik Malik wrote:come on fred, how is it done?
Patience Is A Virtue


Ok, sorry
I'll be quiet as a mouse until he comes...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I don't think it is possible to listen for key presses on a command line (though I would be happy to be proven wrong). An approach I have thought about, but never implemented yet, would be to have a period System.in read which checks for specific keys being entered. The key would be to make sure the System.in read would timeout after a very short period so the app doesn't have to always wait for user input. The problem with that is that System IO is often un-interruptable, so the job of making a timeout is much harder.

I think I have an example of an interruptable stream hanging around which can be used to wrap around System.in and add timeout to user input reads. I will search around to see if I can find the code.


Steve
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

you can use multi threading here.

your main thread will print the output forever.

another thread created by you will wait for a particular key or any key to be pressed from the keyboard.

try to implement it , i have done it many times in networking examples.

avi sinha.


SCJP 5.0 SCWCD 5.0
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
avi sinha wrote:you can use multi threading here.

your main thread will print the output forever.

another thread created by you will wait for a particular key or any key to be pressed from the keyboard.

try to implement it , i have done it many times in my lab .

avi sinha.


Thank you very much, I'm not that good at threads, so could you please give an example code for that, while I'm reading about threads...
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452



I am a bit busy right now that's why i didn't handle the exceptions properly.
always try to handle exceptions properly and use naming convention as much as possible.
hope this code will solve your problem.

avi sinha
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
avi sinha wrote:

I am a bit busy right now that's why i didn't handle the exceptions properly.
always try to handle exceptions properly and use naming convention as much as possible.
hope this code will solve your problem.

avi sinha


Thank you very much, this is exactly what I needed...

and thanks for other guys as well..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
That looks good. Wouldn't you use a boolean rather than an int for the marker variable, however?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

Campbell Ritchie wrote:That looks good. Wouldn't you use a boolean rather than an int for the marker variable, however?


of course sir and using boolean will make the code more readable. actually my exams are going on and i didn't want to tell him the actual code. so i wrote the code in a hurry. exam-time


avi sinha

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to keep a program running until the user terminates it?