private void WaitForConnectionCallBack(IAsyncResult iar)
{
try
{
// Get the pipe
var pipeServer = (NamedPipeServerStream) iar.AsyncState;
// End waiting for the connection
pipeServer.EndWaitForConnection(iar);
var buffer = new byte[4096];
// Read the incoming message
pipeServer.Read(buffer, 0, 4096);
// Convert byte buffer to string
var stringData = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
// Pass message back to calling form
PipeMessage?.Invoke(stringData);
// Kill original sever and create new wait server
pipeServer.Close();
pipeServer = GetPipeServer(_pipeName);
// Recursively wait for the connection again and again....
_pipeServer = pipeServer;
pipeServer.BeginWaitForConnection(WaitForConnectionCallBack, pipeServer);
}
catch (Exception e)
{
if (!_closed)
_logger.Error(e, "Exception in named pipe '{0}'", _pipeName);
}
}