Skip to content

Commit 45bc231

Browse files
committed
remove all threads when the app is closed
1 parent c11b5da commit 45bc231

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/event_handler/debug.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{collections::HashMap, rc::Rc};
1+
use std::{collections::HashMap, process::exit, rc::Rc, time::Duration};
22

33
use crate::slint_generatedAppWindow::{raw_can, AppWindow};
4-
use slint::{Model, SharedString, VecModel, Weak};
4+
use slint::{ComponentHandle, Model, SharedString, VecModel, Weak};
55
use socketcan::{CanSocket, EmbeddedFrame, Frame, Socket};
66
pub struct DebugHandler<'a> {
77
#[cfg(target_os = "linux")]
@@ -14,6 +14,14 @@ pub struct DebugHandler<'a> {
1414
}
1515

1616
impl<'a> DebugHandler<'a> {
17+
pub fn check_to_kill_thread(&self) {
18+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
19+
if !ui.window().is_visible() {
20+
exit(1);
21+
}
22+
});
23+
}
24+
1725
pub fn run(&mut self) {
1826
let can_socket = CanSocket::open(self.iface).unwrap();
1927
if let Ok(frame) = can_socket.read_frame() {
@@ -38,6 +46,7 @@ impl<'a> DebugHandler<'a> {
3846
});
3947
}
4048
}
49+
std::thread::sleep(Duration::from_millis(1));
4150
}
4251

4352
fn bitrate(&self) -> Option<u32> {

src/event_handler/view.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use can_dbc::DBC;
22
use chrono::Utc;
33
#[cfg(target_os = "windows")]
44
use pcan_basic::socket::usb::UsbCanSocket;
5-
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
5+
use slint::{ComponentHandle, Model, ModelRc, SharedString, VecModel, Weak};
66
#[cfg(target_os = "linux")]
77
use socketcan::{CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
88
use std::{
99
collections::HashMap,
1010
fmt::Write,
11+
process::exit,
1112
rc::Rc,
1213
sync::{mpsc::Receiver, Arc, Mutex},
1314
thread::sleep,
@@ -29,6 +30,13 @@ static mut NEW_DBC_CHECK: bool = false;
2930
use super::{EVEN_COLOR, ODD_COLOR};
3031

3132
impl<'a> ViewHandler<'a> {
33+
pub fn check_to_kill_thread(&self) {
34+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
35+
if !ui.window().is_visible() {
36+
exit(1);
37+
}
38+
});
39+
}
3240
pub fn process_can_messages(&mut self) {
3341
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
3442
#[cfg(target_os = "linux")]
@@ -68,6 +76,7 @@ impl<'a> ViewHandler<'a> {
6876
let mut start_bus_load = Instant::now();
6977
let mut total_bits = 0;
7078
loop {
79+
self.check_to_kill_thread();
7180
let bus_state = match can_if.state().unwrap().unwrap() {
7281
socketcan::nl::CanState::ErrorActive => "ERR_ACTIVE",
7382
socketcan::nl::CanState::ErrorWarning => "ERR_WARNING",
@@ -138,6 +147,7 @@ impl<'a> ViewHandler<'a> {
138147
use pcan_basic::{error::PcanError, socket::RecvCan};
139148
let mut start_bus_load = Instant::now();
140149
let mut total_bits = 0;
150+
self.check_to_kill_thread();
141151
loop {
142152
let bitrate = self.bitrate().unwrap();
143153
let busload = if start_bus_load.elapsed() >= Duration::from_millis(1000) {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ async fn main() -> io::Result<()> {
118118
};
119119
loop {
120120
can_handler.run();
121+
can_handler.check_to_kill_thread();
121122
}
122123
}
123124
});

0 commit comments

Comments
 (0)