public void Connect(string pipeName, string serverName = ".", int timeout = 2000) { NamedPipeClientStream pipeStream = null; try { pipeStream = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation); pipeStream.Connect(timeout); using (var stringStream = new StringStream(pipeStream)) { if (OnConnected != null) { OnConnected(stringStream); } } } catch (Exception exception) { if (OnErrorOcurred != null) { OnErrorOcurred(exception); } } finally { if (pipeStream != null && pipeStream.IsConnected) { pipeStream.Flush(); pipeStream.Close(); } } }
private void ProcessClientThread(object o) { using (var pipeStream = (NamedPipeServerStream)o) { try { using (var stringStream = new StringStream(pipeStream)) { if (OnClientConnected != null) { OnClientConnected(stringStream); } } } catch (Exception exception) { if (OnErrorOcurred != null) { OnErrorOcurred(exception); } } finally { if (pipeStream != null && pipeStream.IsConnected) { pipeStream.Flush(); pipeStream.Close(); } } } }