I have a named pipe server similar to the MSDN sample at http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx and would like to allow clients to send an "exit" message which causes the server to gracefully stop.
So in the "InstanceThread()", if a special message is received, I would like to make the server stop.
I tried to stop the call to ConnectNamedPipe() in the main thread from the separate thread for "InstanceThread()" by closing the pipe handle, but this does not work.
I already tried various things, among others closing the overall pipe, exiting directly from the InstanceThread, ... but none of them causes the call to ConnectNamedPipe() to stop.
I played with SetNamedPipeHandleState(), but it complicates the implementation hugely, also using overlapped I/O seems overkill for this simple requirement.
So is there an easier way to get ConnectNamedPipe() to return when the server process should be stopped and not wait endlessly for client connections?
If you don't need to support Windows XP, you could try using CancelSynchronousIo.
If the process is exiting, you don't need to do anything; the thread will be terminated when Windows tears down the process.
Alternatively, you could make the call to ConnectNamedPipe exit simply by connecting to the named pipe yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With