Fork me on GitHub
#cljfx
<
2020-10-31
>
jpmonettas13:10:03

what is the correct way of exiting the application when the user closes the main window? Where should I call shutdown-agents of Platform/exit ?

vlaaad13:10:33

You can set implicit exit to true to exit the platform on last window close, see https://github.com/cljfx/hn/blob/master/src/hn/core.clj#L49

vlaaad13:10:00

I think it makes sense to call it in -main so it's not called during development

vlaaad13:10:37

Do you use agents for cljfx's wrap async only?

vlaaad13:10:11

If yes, I would suggest removing wrap-async, it creates more problems than solves

jpmonettas14:10:25

I already added (Platform/setImplicitExit true) but the app is still not exiting after I closed the main window, maybe because some threads still running

jpmonettas14:10:49

I'm not using wrap-async

jpmonettas15:10:18

what I'm doing now as a workaround is setting stage with :on-close-request (fn [& _] (System/exit 0))

vlaaad15:10:07

Looks fine, though I would expect calling platform/exit and shutdown-agents there to let jvm quit

jpmonettas16:10:08

tried that, but platform/exit doesn't work, it complains about not being called in the correct thread, tried with run-later but didn't work either

Remy14:11:34

have you tried running that last one inside a fx/on-fx-thread ?https://github.com/cljfx/cljfx/blob/master/src/cljfx/platform.clj#L24

vlaaad14:11:03

run-later and on-fx-thread are pretty similar 🙂

truestory 3
Remy14:11:54

Just to give a bit more detail. When calling :on-close-request (fn [& _] (fx/on-fx-thread (javafx.application.Platform/exit))) and setting up (javafx.application.Platform/setImplicitExit true) inside my -main

Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread
	at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
	at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:446)
	at javafx.stage.Window.setShowing(Window.java:1173)
	at javafx.stage.Window.hide(Window.java:1199)
	at com.sun.javafx.stage.WindowCloseRequestHandler.dispatchBubblingEvent(WindowCloseRequestHandler.java:45)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at com.sun.javafx.stage.WindowPeerListener.closing(WindowPeerListener.java:93)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:147)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$4(GlassWindowEventHandler.java:176)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:174)
	at com.sun.glass.ui.Window.handleWindowEvent(Window.java:1351)
	at com.sun.glass.ui.Window.notifyClose(Window.java:1251)
	at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
	at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
	at java.base/java.lang.Thread.run(Thread.java:832)

vlaaad14:11:53

> java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread

Remy14:11:09

my thoughts exactly 😄

Remy14:11:50

I started seeing this kind of thread related issues when switching to fx/create-app

Remy14:11:47

What I mean is I was forced to use on-fx-thread for (.showDialog (DirectryChooser.) window)

vlaaad14:11:07

argh, create-app...

vlaaad14:11:23

I made a mistake introducing fx/wrap-async

vlaaad14:11:35

it only creates problems

vlaaad14:11:54

it's deprecated now, but it's in create-app too...

3