Skip to content

Commit 2607d6f

Browse files
Förbättringar i HelloController och HelloFX
1 parent cc87a6a commit 2607d6f

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/main/java/com/example/HelloFX.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/com/example/ImageServer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private void startServer() throws IOException {
7878
public void stop() {
7979
if (server != null) {
8080
server.stop(0);
81-
System.out.println("🛑 Image server stopped");
8281
}
8382
}
8483
}

0 commit comments

Comments
 (0)