I am developing a file explorer and i want to implement search function in another thread, but i want it to be stoppable on signal.
its tauri project so it has window object which listens to events from frontend
is there a way to stop thread execution on signal or something?
fn search_in_dir(window: Arc<Mutex<Window>>, dir: String, pattern: String) {
let pattern = pattern.to_lowercase();
window
.lock()
.unwrap()
.emit("search-status", SearchStatus::Searching)
.unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let thread = std::thread::spawn(move || {
let entries = WalkDir::new(Path::new(&dir))
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok());
for entry in entries {
if let Some(fname) = entry.file_name().to_str() {
if fname.to_lowercase().contains(&pattern) {
let f = File {
name: fname.to_string(),
path: entry.path().to_str().expect("invalid path").to_string(),
is_dir: entry.file_type().is_dir(),
size: entry.metadata().expect("invalid metadata").len(),
};
tx.send(f).unwrap();
}
}
}
});
for received in rx {
println!("File: {:?}", &received);
let _ = window.lock().unwrap().emit("file-found", received).unwrap();
}
let _ = thread.join();
}
i tried to put mpsc channel in thread but i couldn’t get it through the compiler (i am still learning rust)
New contributor
Dmitry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.