PipeServer
FormLayout

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
namespace pipeServer
{
public partial class Form1:Form
{
private delegate void MonitorPipeDelegate();
void MonitorPipe()
{
while(true)
{
using(NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe",PipeDirection.InOut,1,PipeTransmissionMode.Message, PipeOptions.Asynchronous))
{
if(pipeServer.IsConnected)
{
using(StreamReader sr = new StreamReader(pipeServer))
{
if(sr != null)
this.Invoke((MethodInvoker)delegate { lsvMessage.Items.Add(sr.ReadLine()); });
}
pipeServer.Close();
}
}
}
}
private void Form1()
{
InitializeComponent();
MonitorPipeDelegate d1 = MonitorPipe;
d1.BeginInvoke(null, null);
}
}
}
pipeClient
Form Layout

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
namespace pippeclient
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream("localhost", "testpipe",
PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None))
{
try
{
pipeClient.Connect(5000);
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
if(sw != null)
sw.WriteLine(this.txtMessage.Text);
else
MessageBox.Show("not connected");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
pipeClient.Close(0);
}
}
}
}