From aa277adcc0bd4fab904429b0cfefe5968bddc0a8 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Sun, 15 Jan 2023 13:40:02 -0500 Subject: [PATCH] Added a background task to handle updating the system information --- src/main/java/net/metaunix/App.java | 7 +++++++ .../net/metaunix/controllers/HomeController.java | 6 ++++++ .../net/metaunix/tasks/UpdateSystemTask.java | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 src/main/java/net/metaunix/tasks/UpdateSystemTask.java diff --git a/src/main/java/net/metaunix/App.java b/src/main/java/net/metaunix/App.java index fa0c9b3..94d13c1 100644 --- a/src/main/java/net/metaunix/App.java +++ b/src/main/java/net/metaunix/App.java @@ -11,6 +11,8 @@ import javafx.stage.Stage; import oshi.SystemInfo; import oshi.software.os.OperatingSystem; +import net.metaunix.controllers.HomeController; + /** * JavaFX App @@ -35,6 +37,11 @@ public class App extends Application { stage.show(); } + @Override + public void stop() { + HomeController.ust.cancel(); + } + public static void main(String[] args) { launch(); } diff --git a/src/main/java/net/metaunix/controllers/HomeController.java b/src/main/java/net/metaunix/controllers/HomeController.java index 6e3b78e..b6f275b 100644 --- a/src/main/java/net/metaunix/controllers/HomeController.java +++ b/src/main/java/net/metaunix/controllers/HomeController.java @@ -5,8 +5,12 @@ import javafx.scene.control.Label; import oshi.SystemInfo; import oshi.software.os.OperatingSystem; +import net.metaunix.tasks.UpdateSystemTask; + public class HomeController { + public static UpdateSystemTask ust = new UpdateSystemTask(); + @FXML private Label osInfo; @@ -15,6 +19,8 @@ public class HomeController { SystemInfo si = new SystemInfo(); OperatingSystem os = si.getOperatingSystem(); osInfo.setText(String.valueOf(os)); + + new Thread(this.ust).start(); } } diff --git a/src/main/java/net/metaunix/tasks/UpdateSystemTask.java b/src/main/java/net/metaunix/tasks/UpdateSystemTask.java new file mode 100644 index 0000000..ec15827 --- /dev/null +++ b/src/main/java/net/metaunix/tasks/UpdateSystemTask.java @@ -0,0 +1,16 @@ +package net.metaunix.tasks; + +import java.lang.Thread; +import javafx.concurrent.Task; + +public class UpdateSystemTask extends Task { + + @Override + protected Void call() throws Exception { + while (true) { + System.out.println("Test"); + Thread.sleep(2000); + } + } + +}