@@ -17,15 +17,15 @@ public void start(Stage stage) throws IOException {
1717 // Skapa en enda NtfyConnection-instans
1818 connection = new NtfyConnectionImpl ();
1919
20- // Starta ImageServer på separat tråd
20+ // Starta ImageServer på separat daemon- tråd
2121 Thread serverThread = new Thread (() -> {
2222 try {
2323 imageServer = new ImageServer (8081 );
2424 } catch (IOException e ) {
2525 e .printStackTrace ();
2626 }
2727 });
28- serverThread .setDaemon (true ); // avslutas automatiskt vid app-stopp
28+ serverThread .setDaemon (true );
2929 serverThread .start ();
3030
3131 // Ladda FXML
@@ -40,25 +40,29 @@ public void start(Stage stage) throws IOException {
4040 stage .setScene (scene );
4141 stage .show ();
4242
43- // Säkerställ stängning av server och connection vid fönsterstängning
43+ // Säkerställ att connection och server stoppas vid stängning
4444 stage .setOnCloseRequest (event -> {
4545 System .out .println ("🛑 Application closing..." );
4646
47- if ( connection != null ) {
48- connection . stopReceiving ();
49- System . out . println ( "🔌 NtfyConnection stopped" );
50- }
51-
52- if ( imageServer != null ) {
53- imageServer . stop ();
47+ try {
48+ if ( connection != null ) {
49+ connection . stopReceiving ( );
50+ System . out . println ( "🔌 NtfyConnection stopped" );
51+ }
52+ } catch ( Exception e ) {
53+ e . printStackTrace ();
5454 }
5555
56- // Vänta på server-trådens avslut (valfritt)
5756 try {
58- serverThread .join (500 ); // max 0.5 sekunder
59- } catch (InterruptedException e ) {
57+ if (imageServer != null ) {
58+ imageServer .stop ();
59+ System .out .println ("🛑 Image server stopped" );
60+ }
61+ } catch (Exception e ) {
6062 e .printStackTrace ();
6163 }
64+
65+ // Ingen need att joina daemon-tråden; den avslutas automatiskt
6266 });
6367 }
6468
0 commit comments