Fixed the periodic update

This commit is contained in:
Gregory Ballantine 2023-01-15 22:58:03 -05:00
parent aa277adcc0
commit 2613f8a529
5 changed files with 45 additions and 30 deletions

View File

@ -37,11 +37,6 @@ public class App extends Application {
stage.show();
}
@Override
public void stop() {
HomeController.ust.cancel();
}
public static void main(String[] args) {
launch();
}

View File

@ -2,6 +2,8 @@ package net.metaunix;
public class SysInfo {
public static String osString = "N/a";
public static String javaVersion() {
return System.getProperty("java.version");
}

View File

@ -2,25 +2,29 @@ package net.metaunix.controllers;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;
import javafx.util.Duration;
import net.metaunix.tasks.UpdateSystemTask;
import net.metaunix.SysInfo;
import net.metaunix.services.UpdateSystemInfoService;
public class HomeController {
public static UpdateSystemTask ust = new UpdateSystemTask();
@FXML
private Label osInfo;
@FXML
public void initialize() {
SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
osInfo.setText(String.valueOf(os));
UpdateSystemInfoService usis = new UpdateSystemInfoService();
usis.setOnSucceeded((e) -> {
updateLabels();
});
usis.setPeriod(Duration.seconds(3));
usis.start();
}
new Thread(this.ust).start();
private void updateLabels() {
osInfo.setText(SysInfo.osString);
}
}

View File

@ -0,0 +1,30 @@
package net.metaunix.services;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;
import net.metaunix.SysInfo;
public class UpdateSystemInfoService extends ScheduledService<Void> {
private static int counter = 0;
@Override
protected Task<Void> createTask() {
return new Task<>() {
@Override
protected Void call() throws Exception {
UpdateSystemInfoService.counter++;
SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
SysInfo.osString = String.valueOf(UpdateSystemInfoService.counter) + " " + String.valueOf(os);
return null;
}
};
}
}

View File

@ -1,16 +0,0 @@
package net.metaunix.tasks;
import java.lang.Thread;
import javafx.concurrent.Task;
public class UpdateSystemTask extends Task<Void> {
@Override
protected Void call() throws Exception {
while (true) {
System.out.println("Test");
Thread.sleep(2000);
}
}
}