6

System.IO.Pipes (コンソール アプリと GUI アプリ) を使用して、同じコンピューター上の 2 つの個別のプロセス間の通信を確立しました。コンソール アプリ NamedPipeServerStream はパイプを作成し、GUI アプリ NamedPipeClientStream は既存のパイプに接続します。GUI を頻繁に更新しています。私の質問は、名前付きパイプ手法がこの状況を処理する最も効率的な方法であるかということです。2 番目の質問は、Reactive Extensions RX がこの状況により適しているか、またどのように適合するかということです。前もって感謝します。

サーバ

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace PipeApplicationSender
{

    class ProgramPipeTest
    {
       static void Main(string[] args)
        {

            ProgramPipeTest Server = new ProgramPipeTest();

            Thread ServerThread = new Thread( Server.ThreadStartServer );

            ServerThread.Start();
        }


        public void ThreadStartServer()
        {
            // Create a name pipe
            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
            {

                // Wait for a connection
                pipeStream.WaitForConnection();
                Console.WriteLine("[Server] Pipe connection established");

                using (StreamReader sr = new StreamReader(pipeStream))
                {
                    string temp;
                    // We read a line from the pipe and print it together with the current time
                    while ((temp = sr.ReadLine()) != null)
                    {
                        Console.WriteLine("{0}: {1}", DateTime.Now, temp);
                    }
                }
            }
        }

クライアント

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace PipeApplicationReceiver
{

    class ProgramPipeReceive
    {
       static void Main(string[] args)
        {

            ProgramPipeReceive Server = new ProgramPipeReceive ();

            Thread ServerThread = new Thread( Server.ThreadStartServer );

            ServerThread.Start();
        }


        public void ThreadStartClient(object obj)
        {
            // Ensure that we only start the client after the server has created the pipe
            ManualResetEvent SyncClientServer = (ManualResetEvent)obj;

            // Only continue after the server was created -- otherwise we just fail badly
            // SyncClientServer.WaitOne();

            using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("mytestpipe"))
            {
                // The connect function will indefinately wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect();

                Console.WriteLine("[Client] Pipe connection established");
                using (StreamWriter sw = new StreamWriter(pipeStream))
                {
                    sw.AutoFlush = true;
                    string temp;
                  while ((temp = Console.ReadLine()) != null)
                    {
                        if (temp == "quit") break;
                        sw.WriteLine(temp);
                    }
                }
            }
        }


    }
}
4

2 に答える 2