From aa7c3043f37280796b15eabd7d199b0eb721b12e Mon Sep 17 00:00:00 2001 From: Nigel Sim Date: Tue, 23 Oct 2012 21:38:21 +1000 Subject: [PATCH] Remove System.exit to avoid strange behavour. --- .../src/org/boris/winrun4j/MainService.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/org.boris.winrun4j/java/src/org/boris/winrun4j/MainService.java b/org.boris.winrun4j/java/src/org/boris/winrun4j/MainService.java index 9ce0c52..36fcc61 100644 --- a/org.boris.winrun4j/java/src/org/boris/winrun4j/MainService.java +++ b/org.boris.winrun4j/java/src/org/boris/winrun4j/MainService.java @@ -14,12 +14,28 @@ public class MainService implements Service { private String serviceClass = INI.getProperty("MainService:class"); + private volatile boolean shutdown = false; - public int serviceMain(String[] args) throws ServiceException { + public int serviceMain(final String[] args) throws ServiceException { try { Class c = Class.forName(serviceClass); - Method m = c.getMethod("main", String[].class); - m.invoke(null, (Object) args); + final Method m = c.getMethod("main", String[].class); + // Create a thread to run the service in, and use this thread to monitor it. + Thread t = new Thread() { + public void run() { + try { + m.invoke(null, (Object) args); + } catch (Exception e) { + } + shutdown = true; + } + }; + t.start(); + while (!shutdown) { + try { + Thread.sleep(5000); + } catch (InterruptedException e) {} + } } catch (Exception e) { throw new ServiceException(e); } @@ -30,7 +46,7 @@ public int serviceRequest(int control) throws ServiceException { switch (control) { case SERVICE_CONTROL_STOP: case SERVICE_CONTROL_SHUTDOWN: - System.exit(0); + shutdown = true; break; default: break;