简体   繁体   English

如何正常停止正在Windows管道上监听的服务器进程

[英]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. 我在http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365588(v=vs.85).aspx上有一个类似于MSDN示例的命名管道服务器,并且希望允许客户端发送导致服务器正常停止的“退出”消息。

So in the "InstanceThread()", if a special message is received, I would like to make the server stop. 因此,在“ InstanceThread()”中,如果收到特殊消息,我想使服务器停止。

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. 我试图通过关闭管道句柄来停止从“ InstanceThread()”的单独线程停止对主线程中ConnectNamedPipe()的调用,但这不起作用。

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. 我已经尝试了各种方法,其中包括关闭整个管道,直接从InstanceThread中退出,...但是没有一个方法导致对ConnectNamedPipe()的调用停止。

I played with SetNamedPipeHandleState(), but it complicates the implementation hugely, also using overlapped I/O seems overkill for this simple requirement. 我使用了SetNamedPipeHandleState(),但是它使实现变得非常复杂,对于这个简单的要求,使用重叠的I / O似乎也太过分了。

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? 那么,是否有一种更简单的方法可以使ConnectNamedPipe()在应该停止服务器进程而不是无休止地等待客户端连接时返回?

If you don't need to support Windows XP, you could try using CancelSynchronousIo . 如果不需要支持Windows XP,则可以尝试使用CancelSynchronousIo

If the process is exiting, you don't need to do anything; 如果该过程正在退出,则无需执行任何操作。 the thread will be terminated when Windows tears down the process. Windows拆除进程时,线程将终止。

Alternatively, you could make the call to ConnectNamedPipe exit simply by connecting to the named pipe yourself. 另外,您可以通过自己直接连接到命名管道来进行对ConnectNamedPipe的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM