質問

2 つのプロセス間で通信するために 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.pipesteam.CheckwriteOperations()at System.io.io.pipesteam.writeByte(byte値)Peachesobjectvisualizer.visualizer.show(idialogvisualizerservice windowservice、ivisualizerobjectprovider objectprovider)

待機が戻ったら、すべての準備が完了しているはずだと思います。

PipeServer.WaitForConnection() を使用するとすべて正常に動作しますが、パイプが接続できない場合にアプリケーションをハングすることはできません。

役に立ちましたか?

解決

電話する必要があります 接続の終了待機.

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

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

    // ...
}

見る: IAsyncResult 設計パターン.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top