How to gracefully stop a server process that is listening on a pipe in Windows

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 a message "exit" which causes the server to stop gracefully.

So, in "InstanceThread ()", if a special message is received, I would like to stop the server.

I tried to stop the ConnectNamedPipe () call in the main thread from a separate thread for "InstanceThread ()" by closing the channel handle, but this will not work.

I already tried different things, including closing the shared channel, exiting directly from the InstanceThread instance ... but none of them call ConnectNamedPipe () to stop.

I played with SetNamedPipeHandleState (), but it makes implementation very difficult, and using overlapped I / O seems redundant for this simple requirement.

So, is there an easier way to return ConnectNamedPipe () when the server process should be stopped and not wait endlessly for client connections?

+4
source share
1 answer

If you do not need support for Windows XP, you can try CancelSynchronousIo .

If the process ends, you do not need to do anything; the thread will be terminated when Windows hides the process.

Alternatively, you can call ConnectNamedPipe simply by connecting to the named pipe yourself.

+6
source

All Articles