pub struct ChangeNotification { /* private fields */ }Expand description
An owned change-notification handle.
Closed with FindCloseChangeNotification on drop. The type exists so that
the close routine travels with the handle rather than being something each
call site has to remember, which is the same shape
windows-threadpool-sys already
needed for wait targets.
Implementations§
Source§impl ChangeNotification
impl ChangeNotification
Sourcepub fn as_raw(&self) -> HANDLE
pub fn as_raw(&self) -> HANDLE
The raw handle, for passing to a wait.
Borrowed, not transferred: it stays owned by this value. Do not pass
it to CloseHandle.
Sourcepub fn rearm(&self) -> Outcome<()>
pub fn rearm(&self) -> Outcome<()>
Rearms the watch after it has signalled.
A notification handle signals once and then stays signalled until it is rearmed, so a consumer that waits in a loop must call this between waits.
§Errors
Returns the raw Win32 code, unaltered.
§Example
Without the rearm, the second wait returns immediately on the first change forever, and a consumer’s loop spins:
use std::fs;
use windows_namespace_request_sys::prepare;
use windows_namespace_request_sys::watch::{NotifyFilter, WatchDirectory};
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
use windows_sys::Win32::System::Threading::WaitForSingleObject;
use wtf_string::Wtf16String;
let directory = std::env::temp_dir().join(format!("wnrs-rearm-{}", std::process::id()));
let _ = fs::remove_dir_all(&directory);
fs::create_dir_all(&directory)?;
let text = directory.to_str().expect("a temporary path is valid UTF-8");
let notification = WatchDirectory::new(prepare(&Wtf16String::from(text))?)
.with_filter(NotifyFilter::FILE_NAME)
.perform()?;
fs::write(directory.join("first.t"), b"x")?;
// SAFETY: the handle is live for the notification's lifetime.
assert_eq!(unsafe { WaitForSingleObject(notification.as_raw(), 5_000) }, WAIT_OBJECT_0);
// The handle stays signalled until this runs.
notification.rearm()?;
fs::write(directory.join("second.t"), b"x")?;
// SAFETY: as above.
assert_eq!(unsafe { WaitForSingleObject(notification.as_raw(), 5_000) }, WAIT_OBJECT_0);