Simple application of C# NamedPipe pipeline

Used for data exchange between two processes on the same computer

1. Declare the pipeline identification name serverName, and the server and client are created based on this

2. Create a server NamedPipeServerStream to obtain the reading data stream (StreamReader) and writing data stream (StreamWriter); then wait for the client to connect serverStream_m.WaitForConnection(); after the client connects, use the reading data stream to read in a loop Cache dataReadLine();

3. Create a client NamedPipeClientStream to obtain the reading data stream (StreamReader) and writing data stream (StreamWriter); then the client connects to the server Connect()

4. Define the communication protocol, that is, receive specified data and do specified things

5. After the server and client write data into the data stream (StreamWriter), they must call the Flush() function to write the data into the cache area.

PS: Open another process in the code

 Process[] processes = Process.GetProcessesByName("Program Name");
foreach (Process process1 in processes)
{
process1.Kill();
}
Process process = Process.Start("program name.exe");

1. Server code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace NamedPipeComm
{
public class PipeServer
{
private NamedPipeServerStream serverStream_m = null;
private StreamReader sReader_m;
private StreamWriter sWriter_m;
private object lock_m = new object();
private IRCameraControl IRCamera_m;

private const string TOTAL_TEMP = "TOTAL";
private const string SINGLE_TEMP = "SINGLE";
private const string IS_CONNECT = "CONNECT";
private const string SHOW = "SHOW";
private const string HIDE = "HIDE";
private const string CLOSE = "CLOSE";

public PipeServer(string serverName, IRCameraControl iRCamera)
{
serverStream_m = new NamedPipeServerStream(serverName);

sReader_m = new StreamReader(serverStream_m);
sWriter_m = new StreamWriter(serverStream_m);

IRCamera_m = iRCamera;
HandleDataReceived();
}
\t\t
/// <summary>
/// Process client data
/// </summary>
/// <param name="ar"></param>
private void HandleDataReceived()
{
Task.Run(delegate
{
serverStream_m.WaitForConnection();
do
{
try
{
if (serverStream_m.IsConnected)
{
string[] data = sReader_m.ReadLine().Split(',');

switch(data[0])
{
case TOTAL_TEMP:
float[] fInfo = IRCamera_m.GetTempInfoTotalROI();
if (fInfo.Length == 3)
{
sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo[0], fInfo[1], fInfo[2]));
sWriter_m.Flush();
}
break;
case SINGLE_TEMP:
try
{
int count = Convert.ToInt32(data[1]);
float[] fInfo1 = IRCamera_m.GetTempInfoROI(count);
if (fInfo1.Length == 3)
{
sWriter_m.WriteLine(string.Format("{0},{1},{2}", fInfo1[0], fInfo1[1], fInfo1[2]));
sWriter_m.Flush();
}
}
catch (Exception ex)
{

}
break;
case IS_CONNECT:
if (IRCamera_m.IsConnect)
{
sWriter_m.WriteLine(IS_CONNECT);
sWriter_m.Flush();
}
break;
case SHOW:
IRCamera_m.ShowForm();
break;
case CLOSE:
if (IRCamera_m.frmIRAY_m != null)
{
IRCamera_m.frmIRAY_m.Close();
}
else
{
IRCamera_m.ReleaseSDK();
}
break;
case HIDE:
if (IRCamera_m.frmIRAY_m != null)
{
IRCamera_m.frmIRAY_m.Hide();
}
break;
}
}
}
catch (Exception ex)
{

}
Thread.Sleep(10);
} while (true);
});
}
public void Close()
{
if (sReader_m != null)
{
sReader_m.Close();
sReader_m.Dispose();
sReader_m = null;
}
if (sWriter_m != null)
{
sWriter_m.Close();
sWriter_m.Dispose();
sWriter_m = null;
}
if (serverStream_m != null)
{
serverStream_m.Close();
serverStream_m.Dispose();
serverStream_m = null;
}
}
}
}

2. Client code

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NamedPipeComm
{
public class PipeClient
{
public bool IsConnect
{
get;
set;
}

private NamedPipeClientStream clientStream_m = null;
//private BinaryReader sReader_m;
//private BinaryWriter sWriter_m;
private StreamReader sReader_m;
private StreamWriter sWriter_m;

private const string TOTAL_TEMP = "TOTAL";
private const string SINGLE_TEMP = "SINGLE";
private const string IS_CONNECT = "CONNECT";
private const string SHOW = "SHOW";
private const string HIDE = "HIDE";
private const string CLOSE = "CLOSE";

public PipeClient(string serverName)
{
clientStream_m = new NamedPipeClientStream(serverName);
//sReader_m = new BinaryReader(clientStream_m);
//sWriter_m = new BinaryWriter(clientStream_m);
sReader_m = new StreamReader(clientStream_m);
sWriter_m = new StreamWriter(clientStream_m);
try
{
clientStream_m.Connect(3000);
}
catch (Exception ex)
{

}
}

public bool Open()
{
IsConnect = false;
if (QueryLine(IS_CONNECT) == IS_CONNECT)
{
IsConnect = true;
return IsConnect;
}
return IsConnect;
}
public void Close()
{
if (sReader_m != null)
{
sReader_m.Close();
sReader_m.Dispose();
sReader_m = null;
}
if (sWriter_m != null)
{
sWriter_m.Close();
sWriter_m.Dispose();
sWriter_m = null;
}
if (clientStream_m != null)
{
clientStream_m.Close();
clientStream_m.Dispose();
clientStream_m = null;
}
}
public string QueryLine(string str)
{
string result = "";
try
{
sWriter_m.WriteLine(str);
sWriter_m.Flush();
DateTime start = DateTime.Now;
do
{
result = sReader_m.ReadLine();

if (start.Subtract(DateTime.Now).TotalSeconds > 5 || result != "")
{
break;
}
} while (true);
}
catch (Exception ex)
{
}
return result;
}

/// <summary>
/// Get ROI temperature information, return value index: 0=mean, 1=minimum, 2=maximum
/// </summary>
/// <param name="index">ROI index 0~, note that it must be less than the number of selected ROIs, otherwise error data will be returned</param>
/// <returns></returns>
public float[] GetTempInfoROI(int index)
{
float[] value = new float[3];
string write = SINGLE_TEMP + "," + index.ToString();
string[] reads = QueryLine(write).Split(',');
try
{
value[0] = Convert.ToSingle(reads[0]);
value[1] = Convert.ToSingle(reads[1]);
value[2] = Convert.ToSingle(reads[2]);
}
catch (Exception ex)
{

}
return value;
}

/// <summary>
/// Get the ROI total temperature information, return value index: 0=mean, 1=minimum, 2=maximum
/// </summary>
/// <returns></returns>
public float[] GetTempInfoTotalROI()
{
float[] value = new float[3];
string write = TOTAL_TEMP;
string[] reads = QueryLine(write).Split(',');
try
{
value[0] = Convert.ToSingle(reads[0]);
value[1] = Convert.ToSingle(reads[1]);
value[2] = Convert.ToSingle(reads[2]);
}
catch (Exception ex)
{

}
return value;
}

public void ShowForm()
{
try
{
sWriter_m.WriteLine(SHOW);
sWriter_m.Flush();
}
catch (Exception ex)
{
}
}
public void HideForm()
{
try
{
sWriter_m.WriteLine(HIDE);
sWriter_m.Flush();
}
catch (Exception ex)
{
}
}
public void ReleaseSDK()
{
try
{
sWriter_m.WriteLine(CLOSE);
sWriter_m.Flush();
}
catch (Exception ex)
{
}
}
}
}