Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run listener in a thread and stop it? #133

Open
HuakunShen opened this issue May 18, 2024 · 2 comments
Open

How to run listener in a thread and stop it? #133

HuakunShen opened this issue May 18, 2024 · 2 comments

Comments

@HuakunShen
Copy link

How to run the listener in a thread and be able to cancel it some time later?

I tried to use tokio join handle's abort() method but doesn't work.

I am on Mac and in the source code I saw that native APIs were used instead of rust loop, I guess that's why abort() won't work.

Is there any way to do that?

use rdev::{listen, Event};

fn callback(event: Event) {
    println!("My callback {:?}", event);
}

#[tokio::main]
async fn main() {
    let join_handle = tokio::spawn(async move {
        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }

    });
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
    println!("aborting join handle  ");
    join_handle.abort();
    println!("aborted");
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
}
@edraze
Copy link

edraze commented Jun 5, 2024

See here(#72) for more details

@qzd1989
Copy link

qzd1989 commented Jun 30, 2024

#137

I pushed some code to provide stop_listen function on macos~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants