Is there a way to run an Application on a separate thread?

Hi there,

I’m currently working on a plugin for another program. The program and the plugin interact through FFI with C. Therefore, the plugin does not have a traditional entry point like a main function. Instead, the program calls a function to initialize the plugin and another function to shut down the plugin. I would like to use Iced for my plugin in places where a GUI needed. However, for this to work Iced must not block the main thread. Otherwise the program cannot start until the GUI of the plugin is closed.

I tried to run the iced application on a separate thread but it causes the thread to panic with the following error:

Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard. If you absolutely need to create an EventLoop on a different thread, you can use the EventLoopBuilderExtUnix::any_thread function.

Is there a simple solution to this? I don’t have much experience with GUI/frontend programming, so I don’t know much about winit or wgpu.

My code looks something like this:

static mut MAIN_GUI_JOIN_HANDLE: OnceCell<JoinHandle<iced::Result>> = OnceCell::new();

// GUI entry point
pub fn start_gui() {
    let join_handle = thread::spawn(||
    unsafe { MAIN_GUI_JOIN_HANDLE.set(join_handle).unwrap(); }

Disclaimer: I have no idea how to solve this.

But this is an interesting issue indeed. I assume it’s not possible to run the GUI on the main thread and the rest of the code on a different one because the host program will try to interact with your code exclusively through the main thread.

Have you tried reading the source code of iced’s run function to check if maybe you can write your own version using EventLoopBuilderExtUnix::any_thread like the error suggests?