File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
come on fred, how is it done?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulmalik Malik wrote:come on fred, how is it done?
Patience Is A Virtue
 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good. Wouldn't you use a boolean rather than an int for the marker variable, however?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic