Updated dispatchAndWait, updated exceptions, update gtkUpdateThread

This commit is contained in:
nathan 2016-12-18 23:21:06 +01:00
parent 6cce5cd1cb
commit 022e3e1036

View File

@ -214,6 +214,7 @@ class Gtk {
// gdk_threads_leave(); would be needed for g_idle_add() // gdk_threads_leave(); would be needed for g_idle_add()
} }
}; };
gtkUpdateThread.setDaemon(false); // explicitly NOT daemon so that this will hold the JVM open as necessary
gtkUpdateThread.setName("GTK Native Event Loop"); gtkUpdateThread.setName("GTK Native Event Loop");
gtkUpdateThread.start(); gtkUpdateThread.start();
} }
@ -252,7 +253,7 @@ class Gtk {
if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) { if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) {
if (SystemTray.DEBUG) { if (SystemTray.DEBUG) {
SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.", SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.",
new Exception()); new Exception(""));
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -276,7 +277,7 @@ class Gtk {
if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) { if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) {
if (SystemTray.DEBUG) { if (SystemTray.DEBUG) {
SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.", SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.",
new Exception()); new Exception(""));
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -299,7 +300,7 @@ class Gtk {
if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) { if (!blockUntilStarted.await(10, TimeUnit.SECONDS)) {
if (SystemTray.DEBUG) { if (SystemTray.DEBUG) {
SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.", SystemTray.logger.error("Something is very wrong. The waitForStartup took longer than expected.",
new Exception()); new Exception(""));
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -374,36 +375,40 @@ class Gtk {
public static public static
void dispatchAndWait(final Runnable runnable) { void dispatchAndWait(final Runnable runnable) {
if (isDispatch.get()) {
// Run directly on the dispatch thread (should not "redispatch" this again)
runnable.run();
} else {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final CountDownLatch countDownLatch = new CountDownLatch(1); Gtk.dispatch(new Runnable() {
@Override
Gtk.dispatch(new Runnable() { public
@Override void run() {
public try {
void run() { runnable.run();
try { } finally {
runnable.run(); countDownLatch.countDown();
} finally { }
countDownLatch.countDown();
} }
} });
});
// this is slightly different than how swing does it. We have a timeout here so that we can make sure that updates on the GUI // this is slightly different than how swing does it. We have a timeout here so that we can make sure that updates on the GUI
// thread occur in REASONABLE time-frames, and alert the user if not. // thread occur in REASONABLE time-frames, and alert the user if not.
try { try {
if (!countDownLatch.await(TIMEOUT, TimeUnit.SECONDS)) { if (!countDownLatch.await(TIMEOUT, TimeUnit.SECONDS)) {
if (SystemTray.DEBUG) { if (SystemTray.DEBUG) {
SystemTray.logger.error("Something is very wrong. The Event Dispatch Queue took longer than " + TIMEOUT + " seconds " + SystemTray.logger.error("Something is very wrong. The Event Dispatch Queue took longer than " + TIMEOUT + " seconds " +
"to complete. Please adjust `SystemTray.TIMEOUT` to a value which better suites your environment.", "to complete. Please adjust `SystemTray.TIMEOUT` to a value which better suites your environment.",
new Exception()); new Exception(""));
} else { } else {
throw new RuntimeException("Something is very wrong. The Event Dispatch Queue took longer than " + TIMEOUT + " seconds " + throw new RuntimeException("Something is very wrong. The Event Dispatch Queue took longer than " + TIMEOUT + " seconds " +
"to complete. Please adjust `SystemTray.TIMEOUT` to a value which better suites your environment."); "to complete. Please adjust `SystemTray.TIMEOUT` to a value which better suites your environment.");
}
} }
} catch (InterruptedException e) {
SystemTray.logger.error("Error waiting for dispatch to complete.", new Exception(""));
} }
} catch (InterruptedException e) {
SystemTray.logger.error("Error waiting for dispatch to complete.", new Exception());
} }
} }