Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Pipes with Reactive Extensions

I’ve established communication between two separate processes on the same computer using System.IO.Pipes (console app and GUI app). The console app NamedPipeServerStream creates a pipe while the GUI app NamedPipeClientStream connects to an existing pipe. I have very frequent updates of the GUI. My question is, is the Named Pipes technique most efficient way to handle this situation. The second question is would Reactive Extensions RX fit better for this situation and how? Thank You in advance.

Server

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);
                    }
                }
            }
        }

Client

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);
                    }
                }
            }
        }


    }
}
like image 415
Jim Avatar asked Jul 16 '13 11:07

Jim


1 Answers

https://gist.github.com/hanishi/7139122

I just made a complete IpcServer and IpcClient with Rx. I hope you like it.

like image 132
hanishi Avatar answered Oct 13 '22 00:10

hanishi