1

Say I run a program in my Process class, something like:

Process p1 = Runtime.getRuntime().exec("setup.exe"); try { p1.waitFor(); } catch (InterruptedException e) { //Whatever } 

And then I also have a cancel button in my GUI class, something along the lines of:

private void cancelButtonActionPerformed(ActionEvent e) { //Need to interrupt the thread from here! System.exit(0); } 

Simply exiting the program out as is leaves my setup.exe process I created over in Process.java running, and that's a problem. Normally I'd call p1.destroy(), but I'm having issues making the connection in between the two classes.

Any suggestions would be much appreciated!

EDIT:

private void beginThread() { SwingWorker<String, Void> myWorker = new SwingWorker<String, Void>() { @Override protected String doInBackground() throws Exception { //Do some stuff } return null; } }; myWorker.execute(); } 

1 Answer 1

2

Assuming your run the process in a separate thread, you can call processThread.interrupt() from your GUI class.

You then simply need to modify the try/catch to:

try { p1.waitFor(); } catch (InterruptedException e) { Sys.err(e.toString()); //don't ignore the exception: p1.destroy(); Thread.currentThread.interrupt(); //reset interrupted flag } 
Sign up to request clarification or add additional context in comments.

15 Comments

Your assumption is right. Couple questions, though. First, since I've never used interrupt() before, is this going to otherwise affect any other programs or processes that run later on in the thread? I'm going to need to repeat this whole thing with a number of other processes as well, as I run about half a dozen in total. The second question, what's wrong with pushing a quick log of the exception? Thanks!
@JTApps my comment meant: when you get an interruption, don't ignore it. You can log it of course!
When you call someThread.interrupt() you set that thread's interrupted flag to true. That's it. It is then that thread's responsibility to do something with it. In your case, p1.waitFor() throws an InterruptedException when it detects that the thread in which it is running has been interrupted and you can decide to ignore it or do something with it. Resetting the interrupted flag is good practice if, for example, your code is run in an executor. If it is a stand alone thread and there are no more instructions after the catch block, then it is essentially a no-op.
I ended up running into a problem. This is my first time working with multiple threads, so please bear with me. I added some additional code to my original post. It's the thread where this process is running, and it seems like that's the one I want to interrupt. However, nothing I've tried thus far has let me interrupt the thread from my cancel button method. They are both in the same class, I should add.
@JTApps Avoid using the suppress warnings thingy: fix your code instead: SwingWorker<String, Void> myWorker= new SwingWorker<String, Void>() { ...}.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.