Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gracefully stop a server process which is listening on a pipe on 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 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?

like image 338
centic Avatar asked Jul 25 '12 13:07

centic


1 Answers

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.

like image 115
Harry Johnston Avatar answered Sep 24 '22 18:09

Harry Johnston