2

currently, I am building a small application with Rust and Tauri but I've got the following issue that I need to solve:

Things that I want to do simultaneously:

  • Checking every 10 sec if a specific application is running
  • Polling every second data from SharedMemory via winapi

Both of them are working fine but I tried to refactor stuff and now I've got the following problem: When my frontend sends me an event that the application is ready (or inside .on_page_load() I want to start both processes I mentioned before:

#[tauri::command]
async fn app_ready(window: tauri::Window) {
    let is_alive = false; // I think this needs to be a mutex or a mutex that is wrapped around Arc::new()

    tokio::join!(
        poll_acc_process(&window, &is_alive),
        handle_phycics(&window, &is_alive),
    );
}

Visual Studio Code is complaining about the following stuff: future cannot be sent between threads safely within impl futures::Future<Output = ()>, the trait std::marker::Send is not implemented for *mut c_void

c_void is the handle of CreateFileMappingW from the winapi crate.

async fn poll_acc_process(window: &Window, is_alive: &bool) {
    loop {
        window.emit("acc_process", is_alive).unwrap();
        tokio::time::sleep(time::Duration::from_secs(10));
    }
}

async fn handle_phycics(window: &Window, is_alive: &bool) {
    while is_alive {
        let s_handle = get_statics_mapped_file(); // _handle represents c_void here
        let s_memory = get_statics_mapview_of_file(s_handle);
        window
            .emit("update_statistics", Statics::new_from_memory(s_memory))
            .unwrap();

        let p_handle = get_physics_mapped_file(); // _handle represents c_void here
        let physics = get_physics_mapview_of_file(p_handle);
        window.emit("update_physics", physics).unwrap();

        if physics.current_max_rpm != 0 {
            let g_handle = get_graphics_mapped_file(); // _handle represents c_void here
            let g_memory = get_graphics_mapview_of_file(g_handle);
            window
                .emit("update_graphics", Graphics::new_from_mem(g_memory))
                .unwrap();
        }
        tokio::time::sleep(time::Duration::from_secs(1)).await;
    }
}

Is it possible to solve my problem somehow this way or should I try another approach?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
RobDeFlop
  • 21
  • 3
  • Windows is event-driven. Whenever you feel the need to poll, there's a better solution. You can request that the system call you back whenever a process is created (or terminated). Polling shared memory isn't usually meaningful either: Have the producer set an event whenever there is data available. Or use some other IPC mechanism. – IInspectable Jul 04 '22 at 17:33
  • The examples that people are providing in the forum about the game are done in C++ and the examples are handling it equivalent in C++ so I just "copied" them. I really don't know any other function/method that could be useful. – RobDeFlop Jul 04 '22 at 17:37
  • Please post the full error message from `cargo check`. – Chayim Friedman Jul 04 '22 at 20:20

0 Answers0