#region Client
Namedpipeclientstream pipeclient =
New Namedpipeclientstream ("localhost", "testpipe", Pipedirection.inout, Pipeoptions.asynchronous, Tokenimpersonationlevel.none);
StreamWriter SW = null;
private void Form1_Load (object sender, EventArgs e)
{
Try
{
Pipeclient.connect (5000);
SW = new StreamWriter (pipeclient);
Sw. AutoFlush = true;
}
catch (Exception ex)
{
MessageBox.Show ("Connection setup failed, make sure that the service-side program has been opened.") ");
This. Close ();
}
}
private void Button1_Click (object sender, EventArgs e)
{
if (sw! = NULL)
{
Sw. WriteLine (This.richTextBox1.Text);
}
Else
{
MessageBox.Show ("The connection is not established and the message cannot be sent. ");
}
}
#endregion
#region Service Side
Namedpipeserverstream Pipeserver =
New Namedpipeserverstream ("Testpipe", Pipedirection.inout, 1, Pipetransmissionmode.message, pipeoptions.asynchronous);
private void Form1_Load (object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem (delegate
{
Pipeserver.beginwaitforconnection ((o) =
{
Namedpipeserverstream pserver = (namedpipeserverstream) o.asyncstate;
Pserver.endwaitforconnection (o);
StreamReader sr = new StreamReader (pserver);
while (true)
{
This. Invoke ((MethodInvoker) Delegate {listView1.Items.Add (Sr. ReadLine ()); });
}
}, Pipeserver);
});
}
#endregion
C # Two interprocess communication (pipeline communication)