Skip to content

Commit f2a5d33

Browse files
committed
Illegal state exception when adding shutdown hook
1 parent 1a7198a commit f2a5d33

File tree

1 file changed

+14
-4
lines changed
  • webcam-capture/src/main/java/com/github/sarxos/webcam

1 file changed

+14
-4
lines changed

webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ public boolean open(boolean async) {
283283

284284
LOG.debug("Webcam is now open {}", getName());
285285

286+
// install shutdown hook
287+
288+
try {
289+
Runtime.getRuntime().addShutdownHook(hook = new WebcamShutdownHook(this));
290+
} catch (IllegalStateException e) {
291+
292+
LOG.debug("Shutdown in progress, do not open device");
293+
LOG.trace(e.getMessage(), e);
294+
295+
close();
296+
297+
return false;
298+
}
299+
286300
// setup non-blocking configuration
287301

288302
if (asynchronous = async) {
@@ -292,10 +306,6 @@ public boolean open(boolean async) {
292306
updater.start();
293307
}
294308

295-
// install shutdown hook
296-
297-
Runtime.getRuntime().addShutdownHook(hook = new WebcamShutdownHook(this));
298-
299309
// notify listeners
300310

301311
WebcamEvent we = new WebcamEvent(WebcamEventType.OPEN, this);

0 commit comments

Comments
 (0)