所有实例忙于命名管道创建的异常 [英] All instances busy exception on named pipe creation

查看:132
本文介绍了所有实例忙于命名管道创建的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,该服务通过命名管道与gui应用程序进行通信.因此,我有一个线程正在运行,等待应用程序连接,如果我执行一次,它将运行良好.但是,如果线程正在创建命名管道流服务器的新实例,则已建立的连接会中断,并且我会得到所有实例忙碌的异常.引发异常的代码片段是这样的:

I have a windows service which is communicating with a gui application via named pipes. 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);

            }
        }

第二次创建新的管道服务器流NamedPipeServerStream ps = new NamedPipeServerStream("mytestp")时,引发异常.

The exception is thrown when creating the new pipe server stream NamedPipeServerStream ps = new NamedPipeServerStream("mytestp") the second time.

我找到了答案,并且在指定最大服务器实例数时可以使用 NamedPipeServerStream ps = new NamedPipeServerStream("mytestp",PipeDirection.Out,10);

I found the answer and it works when the max number of server instances is specified NamedPipeServerStream ps = new NamedPipeServerStream("mytestp",PipeDirection.Out,10);

此默认值似乎是-1.这就引出了另一个但不是那么重要的问题:有人知道为什么当它像蜜蜂1时它是-1而不是1?

The default value for this seems to be -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?

推荐答案

NamedPipeServerStream构造函数有两个重载,它们为maxNumberOfServerInstances变量分配了默认值,即:

There are two overloads of the NamedPipeServerStream constructor that assign a default to the maxNumberOfServerInstances variable, namely:

public NamedPipeServerStream(String pipeName)

public NamedPipeServerStream(String pipeName, PipeDirection direction)

查看参考源证明此默认值为 1 而不是-1. 这说明了您观察到的行为.

Looking at the reference source proves that this default is 1 and not -1. This explains the behavior you observed.

可能的解决方案是:

  1. 使用允许您指定限制的构造函数,并传递大于1的值

  1. use a constructor that allows you to specify the limit, and pass a value larger than 1

与1相同,并使用内置常量NamedPipeServerStream.MaxAllowedServerInstances询问操作系统将能够分配的最大句柄数.

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.

这篇关于所有实例忙于命名管道创建的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆