user2832424
user2832424

Reputation: 43

Can i call SetEvent() on an HANDLE returned by CreateFile()?

I am using ReadDirectoryChangesW() in a thread to monitor file changes and i'm passing the handle returned by CreateFile() to ReadDirectoryChangesW(). I am using the same handle in WaitForSingleObject() to wait for file changes in the directory. Can i call SetEvent() on this HANDLE to manually exit the thread?

Upvotes: 0

Views: 96

Answers (1)

IInspectable
IInspectable

Reputation: 51375

A file handle can be used as a synchronization object in a call to WaitForSingleObject. That doesn't imply that you can use any synchronization object specific API calls, such as SetEvent on it. The documentation for SetEvent is clear on the subject:

hEvent [in]

A handle to the event object. The CreateEvent or OpenEvent function returns this handle.

The canonical solution to terminating a thread that is waiting for events is to create an additional event object, and use WaitForMultipleObjects inside the thread. When the thread needs to terminate, you call SetEvent on that event object. The return value of WaitForMultipleObjects reports, which synchronization object was signaled.

Upvotes: 4

Related Questions