0

IPC を実装しようとしていますが、私が行っていることは、私が見たドキュメントとは多少異なります。

サービスとフォーム アプリケーションで非同期に送信、受信、および応答する必要があります。

//Service1.cs
serverPipe = new NamedPipeServerStream(@"testpipe", PipeDirection.InOut, 
   1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

//LoginForm.cs
clientPipe = new NamedPipeClientStream(@".", @"testpipe", PipeDirection.InOut
    PipeOptions.Asynchronous);

私はこれが双方を設定すると信じています。

一方から他方に書き込むには:

//In Service1.cs and Login.cs
private void pipeWriter()
{
    StreamWriter write = null;
    write = new StreamWriter(clientPipe);

    if (clientPipe.IsConnected)
    {
        write.Write(clientPipe);
        write.Flush();
    }
}

...そして読む:

//In Service1.cs and Login.cs
private void pipeReader()
{
    try
    {
        while (true)
        {
            using (StreamReader sr = new StreamReader(clientPipe))
            {
                string message;

                while ((message = sr.ReadLine()) != null)
                {
                    //read and respond to messages written to stream
                }
            }
        }
    }
    catch (Exception ex)
    {

    }
}

私は正しい直接にいますか?Windows サービスとフォーム アプリケーションの間で通信する別の (より良い) 方法はありますか?

4

1 に答える 1