我创建了一个Windows服务,它有一个组件监听命名管道与用户空间运行的程序进行交互。我已经使用this answer的代码作为多线程服务器实现的基础,但是,我从下面转载的紧密循环中调用ProcessNextClient操作获得了强大的代码味道。是否真的没有更好的方法来知道何时将另一个流的开放添加到命名管道而不是重复捕获IOException并再次尝试?C#中的多线程服务器实现
public void ProcessNextClient()
{
try
{
NamedPipeServerStream pipeStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 254);
pipeStream.WaitForConnection();
//Spawn a new thread for each request and continue waiting
Thread t = new Thread(ProcessClientThread);
t.Start(pipeStream);
}
catch (Exception e)
{//If there are no more avail connections (254 is in use already) then just keep looping until one is avail
}
}
我在找什么,我想是:一种方法来跟踪仍然活动的线程/任务的数量,从而有开放的管道;或者阻止新的NamedPipeServerStream,直到资源可用而不创建螺旋锁。 – psaxton 2013-03-27 18:54:44