[SOLVED] error handling
i have a java program calling a .bat file on an XP system.
the batch file does a bunch of stuff, including things that may often incur unwanted popup windows or prompts that I would like automatically bypassed.
i am wondering if i can somehow time how long it has taken for the batch file to finish and return to my java program. if it has taken more than, say, five minutes, then my java program should simply move on. my program runs overnight, and i don't want to come to my pc in the morning and have to click on an "OK" prompt, then wait for it to finish.
any tips, suggestions or questions are welcome, thanks for looking
Sounds like a job for Threads. Put the batch file caller on one thread and then have another thread that waits some time say 5 minutes and then checks if the batch file thread has finished.
Fancier code could use wait() and notify() and synchronized methods to coordinate the two threads.
Not to mention that you're going to need some (additional) threads to read the output and error streams of the process or it is likely to simply hang anyway.
thanks for the tips,
Do either of you have any syntactical help on using one thread to determine if another thread is finished?
Use a boolean flag. Set true just before the thread exists.
Test if set in the other thread.
To use isAlive() you'd need a reference to the other thread via a variable which cost as much as referencing the boolean flag.
Of course, there are other ways to get references by using names and ThreadGroups.
For the one thread to check whether the other thread is alive, whether with the isAlive method or with a boolean, a reference to the other thread is still needed, so I don't know what you were getting at with that sentence.
So, why "roll your own" when something finished already exists.
I have been using a new thread to make the batch call, and thus have to give it a name. I then use the main thread to check if it is done within 5 minutes. I pretty much have it all figured out now, but I do not know how to terminate the thread if it has failed to return. Java tells me that Thread.stop() is unsafe.
Also, how do i reference the Main thread? Can I reference it from itself?
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
You use call interuppt against a thread, and the thread that gets interuppted needs to catch that and clean itself up, then stop (or even continue again, depends on what you're doing and why you interuppted).
awesome.... thanks for the tips guys, i have it working perfectly!