[英]All instances busy exception on named pipe creation
I have a windows service which is communicating with a gui application via named pipes. 我有一个Windows服务,通过命名管道与gui应用程序进行通信。 Therefor i have a thread running waiting for the app to connect which is running fine if i do it once.
因此我有一个线程正在运行等待应用程序连接,如果我这样做一次运行正常。 But if the thread is creating a new instance of the named pipe stream server the already established connection breaks down and i get the all instances busy exception.
但是,如果线程正在创建命名管道流服务器的新实例,则已建立的连接会中断,并且我将获得所有实例繁忙异常。 The code fragment where the exception is thrown is this:
抛出异常的代码片段是这样的:
class PipeStreamWriter : TextWriter
{
static NamedPipeServerStream _output = null;
static StreamWriter _writer = null;
static Thread myThread = null;
public PipeStreamWriter()
{
if (myThread == null)
{
ThreadStart newThread = new ThreadStart(delegate{WaitForPipeClient();});
myThread = new Thread(newThread);
myThread.Start();
}
}
public static void WaitForPipeClient()
{
Thread.Sleep(25000);
while (true)
{
NamedPipeServerStream ps = new NamedPipeServerStream("mytestp");
ps.WaitForConnection();
_output = ps;
_writer = new StreamWriter(_output);
}
}
The exception is thrown when creating the new pipe server stream NamedPipeServerStream ps = new NamedPipeServerStream("mytestp")
the second time. 第二次创建新管道服务器流
NamedPipeServerStream ps = new NamedPipeServerStream("mytestp")
时抛出异常。
EDIT: 编辑:
I found the answer and it works when the max number of server instances is specified NamedPipeServerStream ps = new NamedPipeServerStream("mytestp",PipeDirection.Out,10);
我找到了答案,当指定了最大服务器实例数时它可以工作
NamedPipeServerStream ps = new NamedPipeServerStream("mytestp",PipeDirection.Out,10);
The default value for this seems to be -1. 此默认值似乎为-1。 Which leads to another but not that important question: Someone knows why it is -1 and not 1 when it behaves like beeing 1?
这导致另一个但不是那么重要的问题:有人知道为什么它是-1而不是1,当它表现得像是1?
There are two overloads of the NamedPipeServerStream
constructor that assign a default to the maxNumberOfServerInstances
variable, namely: NamedPipeServerStream
构造函数有两个重载,它们为maxNumberOfServerInstances
变量赋值,即:
public NamedPipeServerStream(String pipeName)
and 和
public NamedPipeServerStream(String pipeName, PipeDirection direction)
Looking at the reference source proves that this default is 1 and not -1. 查看引用源证明此默认值为1而不是-1。 This explains the behavior you observed.
这解释了您观察到的行为。
Possible solutions are: 可能的解决方案是
use a constructor that allows you to specify the limit, and pass a value larger than 1 使用允许您指定限制的构造函数,并传递大于1的值
same as 1, and use the built-in constant NamedPipeServerStream.MaxAllowedServerInstances
to ask for the maximum number of handles the operating system will be able to allocate. 与1相同,并使用内置常量
NamedPipeServerStream.MaxAllowedServerInstances
来询问操作系统将能够分配的最大句柄数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.