public event CommandAddedEventHandler CommandAdded; // событие получения новой команды protected virtual void OnCommandAdded(CommandAddedEventArgs args) { if (CommandAdded != null) { CommandAdded(this, args); } }
private void ServerThread() { while (m_isWorking) { Thread.Sleep(10); using (m_pipeSteam = new NamedPipeServerStream(m_pipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.None)) { try { m_pipeSteam.WaitForConnection(); try { if (m_cmdArgs.Length > 1 && m_cmdArgs[1] == "-debug") { addHelloCmd(); } else { #if DEBUG addHelloCmd(); #endif } bool lastReadResult = false; int cnt = 0; while (m_isWorking) { if (!lastReadResult) { Thread.Sleep(1); } // запись данных в пайп writingPipe(); // чтение и сохранение данных из пайпа lastReadResult = readingPipe(); DataReader cmd = getNextCommand(); int oldCnt = cnt; while (cmd != null) { cnt++; // зажигание события получения новой команды CommandAddedEventArgs evargs = new CommandAddedEventArgs(cmd); OnCommandAdded(evargs); cmd = getNextCommand(); } Console.WriteLine("GetCommands: " + (cnt - oldCnt).ToString()); if (!m_pipeSteam.IsConnected) { m_pipeSteam.Close(); break; } } } catch (IOException e) { //MessageBox.Show(e.Message, "Pipe connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine("Pipe error: " + e.Message); m_pipeSteam.Close(); ClearQueue(); } } catch (InvalidOperationException e) { Console.WriteLine("Error: " + e.Message); continue; } } } }