using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Pipes; using System.IO; using System.Threading; namespace PipeClientCommunication { class Writer { private NamedPipeClientStream pipeOut; private StreamWriter sw; private Random rand = new Random(); private int counter = 0; public Writer() { pipeOut = new NamedPipeClientStream(".", "testpipe", PipeDirection.Out); Console.WriteLine("Attempting to connect to pipe"); if (pipeOut != null) pipeOut.Connect(); sw = new StreamWriter(pipeOut); sw.AutoFlush = true; Console.WriteLine("Connected!"); } public void Write() { while (true) { try { sw.WriteLine(counter); Console.WriteLine(Thread.CurrentThread.Name + " " + counter++); } catch (IOException e) { } Thread.Sleep(rand.Next(5000)); } } } class PipeClient { static void Main(string[] args) { Writer w = new Writer(); Thread writeThread1 = new Thread(new ThreadStart(w.Write)); writeThread1.Name = "writerThread1"; writeThread1.Start(); Thread writeThread2 = new Thread(new ThreadStart(w.Write)); writeThread2.Name = "writerThread2"; writeThread2.Start(); } } }