-
I have an app which launches Slint window in a sub-thread from its main thread. I use an fn main() {
.........................
let ui_handle = thread::spawn(move || {
let app = MainWindow::new().unwrap();
........................
........................
let app_weak = app.as_weak();
slint::spawn_local(async move {
loop {
let appw = app_weak.clone();
let state_lock = state.lock().unwrap();
println!("STATE: {:?}", *state_lock);
appw.unwrap().set_state((*state_lock).clone());
}
}).unwrap();
......................
......................
}
if quit {
ui_handle.join().unwrap();
}
} But the main window appears to be blocked by this. I only see a small black window which is unresponsive. What would be the idiomatic and correct way to approach this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I use the let app_weak = app.as_weak();
timer.start(TimerMode::Repeated, Duration::from_millis(5), move || {
let appw = app_weak.clone();
let state_lock = state.lock().unwrap();
appw.unwrap().set_state((*state_lock).clone());
}); It looks like the most idiomatic way to do it. |
Beta Was this translation helpful? Give feedback.
-
You loop never yields (.await) and that's why it blocks the ui thread. |
Beta Was this translation helpful? Give feedback.
-
Ooops I forgot an await there. You're right, the loop is unyielding. And thanks for the let state_notifier: Arc<Notify> = Arc::new(Notify::new());
.................
// State needs updating...
state_notifier.notify_one();
..................
..................
sn_clone = Arc::clone(&state_notifier);
let app_weak = app.as_weak();
slint::spawn_local(async move {
let appw = app_weak.clone();
loop {
sn_clone.notified().await;
let state_lock = state.lock().unwrap();
appw.unwrap().set_state((*state_lock).clone());
}
}).unwrap(); |
Beta Was this translation helpful? Give feedback.
You loop never yields (.await) and that's why it blocks the ui thread.
perhaps this could be solved with tokio::sync::Notify