为什么我的 NamedPipeServerStream 不等待?

发布于 2024-08-28 12:57:24 字数 1522 浏览 12 评论 0原文

我正在使用 NamedPipeServerStream 在两个进程之间进行通信。这是我初始化和连接管道的代码:

void Foo(IHasData objectProvider)
{
    Stream stream = objectProvider.GetData();
    if (stream.Length > 0)
    {
        using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("VisualizerPipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
        {
            string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string uiFileName = Path.Combine(currentDirectory, "VisualizerUIApplication.exe");
            Process.Start(uiFileName);
            if(pipeServer.BeginWaitForConnection(PipeConnected, this).AsyncWaitHandle.WaitOne(5000))
            {
                while (stream.CanRead)
                {
                    pipeServer.WriteByte((byte)stream.ReadByte());
                }
            }
            else
            {
                throw new TimeoutException("Pipe connection to UI process timed out.");
            }
        }
    }
}

private void PipeConnected(IAsyncResult e)
{
}

但它似乎永远不会等待。我不断收到以下异常:

System.InvalidOperationException:管道尚未连接。 在 System.IO.Pipes.PipeStream.CheckWriteOperations() 在System.IO.Pipes.PipeStream.WriteByte(字节值) 在 PeachesObjectVisualizer.Visualizer.Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)

我认为等待返回后一切都应该准备就绪。

如果我使用 pipelineServer.WaitForConnection() 一切正常,但如果管道未连接则挂起应用程序不是一个选项。

I'm working with a NamedPipeServerStream to communicate between two processes. Here is the code where I initialize and connect the pipe:

void Foo(IHasData objectProvider)
{
    Stream stream = objectProvider.GetData();
    if (stream.Length > 0)
    {
        using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("VisualizerPipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
        {
            string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string uiFileName = Path.Combine(currentDirectory, "VisualizerUIApplication.exe");
            Process.Start(uiFileName);
            if(pipeServer.BeginWaitForConnection(PipeConnected, this).AsyncWaitHandle.WaitOne(5000))
            {
                while (stream.CanRead)
                {
                    pipeServer.WriteByte((byte)stream.ReadByte());
                }
            }
            else
            {
                throw new TimeoutException("Pipe connection to UI process timed out.");
            }
        }
    }
}

private void PipeConnected(IAsyncResult e)
{
}

But it never seems to wait. I constantly get the following exception:

System.InvalidOperationException: Pipe hasn't been connected yet.
at System.IO.Pipes.PipeStream.CheckWriteOperations()
at System.IO.Pipes.PipeStream.WriteByte(Byte value)
at PeachesObjectVisualizer.Visualizer.Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)

I would think that after the wait returns everything should be ready to go.

If I use pipeServer.WaitForConnection() everything works fine, but hanging the application if the pipe doesn't connect is not an option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-09-04 12:57:24

您需要调用 结束等待连接

var asyncResult = pipeServer.BeginWaitForConnection(PipeConnected, this);

if (asyncResult.AsyncWaitHandle.WaitOne(5000))
{
    pipeServer.EndWaitForConnection(asyncResult);

    // ...
}

请参阅:IAsyncResult 设计模式

You need to call EndWaitForConnection.

var asyncResult = pipeServer.BeginWaitForConnection(PipeConnected, this);

if (asyncResult.AsyncWaitHandle.WaitOne(5000))
{
    pipeServer.EndWaitForConnection(asyncResult);

    // ...
}

See: IAsyncResult design pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文