Hacker News new | ask | show | jobs
by CJefferson 814 days ago
I have an app where some tasks can take a long time (like a minute in some cases), so I want the gui to continue responding, and also run my long task in a background thread, then update the GUI when it is finished.

Is there any good examples for this, the examples (not unreasonably), mostly seem to do fairly trivial work.

2 comments

The http example at egui.rs uses the `poll_promise` crate to wait for a HTTP request: https://github.com/emilk/egui/blob/master/crates/egui_demo_a...
Thanks!
That’s the key; the thread showing the UI mustn’t do any real work. Instead do the work on another thread and then communicate the status back to the UI thread. Exactly how you do that is up to you; there are plenty of different ways to do it that depend on other choices you’ve made, such as whether you’re using async or not, what async runtime you use, etc, etc.

The very simplest way to do it might be for the UI thread to send the work thread an Arc<AtomicBool>. The work thread can set the bool to true when the work is finished. The UI thread can branch on the value of the bool each frame to decide what to draw. Job done.