Skip to content

Commit

Permalink
remove all threads when the app is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
TuEmb committed Sep 17, 2024
1 parent c11b5da commit 45bc231
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/event_handler/debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, rc::Rc};
use std::{collections::HashMap, process::exit, rc::Rc, time::Duration};

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

impl<'a> DebugHandler<'a> {
pub fn check_to_kill_thread(&self) {
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
if !ui.window().is_visible() {
exit(1);
}
});
}

pub fn run(&mut self) {
let can_socket = CanSocket::open(self.iface).unwrap();
if let Ok(frame) = can_socket.read_frame() {
Expand All @@ -38,6 +46,7 @@ impl<'a> DebugHandler<'a> {
});
}
}
std::thread::sleep(Duration::from_millis(1));
}

fn bitrate(&self) -> Option<u32> {
Expand Down
12 changes: 11 additions & 1 deletion src/event_handler/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use can_dbc::DBC;
use chrono::Utc;
#[cfg(target_os = "windows")]
use pcan_basic::socket::usb::UsbCanSocket;
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
use slint::{ComponentHandle, Model, ModelRc, SharedString, VecModel, Weak};
#[cfg(target_os = "linux")]
use socketcan::{CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
use std::{
collections::HashMap,
fmt::Write,
process::exit,
rc::Rc,
sync::{mpsc::Receiver, Arc, Mutex},
thread::sleep,
Expand All @@ -29,6 +30,13 @@ static mut NEW_DBC_CHECK: bool = false;
use super::{EVEN_COLOR, ODD_COLOR};

impl<'a> ViewHandler<'a> {
pub fn check_to_kill_thread(&self) {
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
if !ui.window().is_visible() {
exit(1);
}
});
}
pub fn process_can_messages(&mut self) {
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -68,6 +76,7 @@ impl<'a> ViewHandler<'a> {
let mut start_bus_load = Instant::now();
let mut total_bits = 0;
loop {
self.check_to_kill_thread();
let bus_state = match can_if.state().unwrap().unwrap() {
socketcan::nl::CanState::ErrorActive => "ERR_ACTIVE",
socketcan::nl::CanState::ErrorWarning => "ERR_WARNING",
Expand Down Expand Up @@ -138,6 +147,7 @@ impl<'a> ViewHandler<'a> {
use pcan_basic::{error::PcanError, socket::RecvCan};
let mut start_bus_load = Instant::now();
let mut total_bits = 0;
self.check_to_kill_thread();
loop {
let bitrate = self.bitrate().unwrap();
let busload = if start_bus_load.elapsed() >= Duration::from_millis(1000) {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async fn main() -> io::Result<()> {
};
loop {
can_handler.run();
can_handler.check_to_kill_thread();
}
}
});
Expand Down

0 comments on commit 45bc231

Please sign in to comment.