询问者
关于匿名通道通信的问题

问题
-
现有个Windows服务,需要启动一个或多个进程并与这些进程定时发送命令信息,由于服务和进程都在本机所以选择使用比较简单的匿名通道进行通信。但是现在遇到一个问题,通道建议之后只能发送一次信息,第一次信息发送完毕之后通道就关闭了,所以程序就没有办法继续进行。 请各位大神帮看一下,是匿名通道本身设计如此,还是我写的代码的问题。
服务端代码如下:
private void timerGlobal_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Logger.LogDebug("定时器开始"); try { if (pipeServer == null) { pipeServer = AnonymousPipeUtil.CreateAnonymousPipe(); } if (process == null || process.HasExited) { string command = Path.Combine(@"E:\TestProject\AnonymousPipeSeverTest\", "AnonymousPipeClientTest.exe"); string para = "/PIPE" + " " + pipeServer.GetClientHandleAsString(); process = StartProcess(command, para, @"E:\TestProject\AnonymousPipeSeverTest\"); } AnonymousPipeUtil.SendMessage(pipeServer, "这个是发送的信息" + DateTime.Now.ToShortTimeString()); } catch (Exception exe) { Logger.LogError("服务出错:" + exe.Message + "\r\n" + exe.StackTrace); } } public static Process StartProcess(string command, string para, string workingDirectory = null) { Process myProcess = new Process(); myProcess.StartInfo.FileName = command; myProcess.StartInfo.Arguments = para; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.StartInfo.CreateNoWindow = true; if (!string.IsNullOrEmpty(workingDirectory)) myProcess.StartInfo.WorkingDirectory = workingDirectory; myProcess.Start(); return myProcess; } public static void SendMessage(AnonymousPipeServerStream pipeServer,string message) { pipeServer.DisposeLocalCopyOfClientHandle(); try { using (StreamWriter sw = new StreamWriter(pipeServer)) { sw.AutoFlush = true; sw.WriteLine(message); pipeServer.WaitForPipeDrain(); } Logger.LogError(string.Format("写入消息:{0}", message)); } catch (IOException e) { Logger.LogError(string.Format("[SERVER] Error: {0}", e.Message)); } }
接收端代码如下:
private static void AnalyzeParameter(string[] args) { try { string command = args[0].ToUpper(); Logger.LogDebug("启用应用程序,command = [" + args[0] + "] [" + args[1] + "]"); if (command.Equals("/PIPE")) { while (true) { //从匿名通道中读取信息 string task = AnonymousPipeUtil.GetServerTask(args[1]); if (string.IsNullOrEmpty(task)) { continue; } if (taskThread == null || taskThread.ThreadState == ThreadState.Stopped) { //启动线程执行命令 ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ThreadRun); taskThread = new Thread(threadStart); taskThread.Start(task); } else { Logger.LogError("由于线程正在处理,抛弃掉当前命令:" + task); } } } } catch (Exception ex) { Logger.LogError("执行AnalyzeParameter出错:" + ex.ToString()); } } public static string GetServerTask(string pipeHandle) { try { StringBuilder sb = new StringBuilder(); using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, pipeHandle)) { try { pipeClient.ReadMode = PipeTransmissionMode.Message; } catch { } using (StreamReader sr = new StreamReader(pipeClient)) { string temp; while ((temp = sr.ReadLine()) != null) { sb.AppendLine(temp); } } } return sb.ToString(); } catch (Exception ex) { Logger.LogError(ex.Message); return ""; } }
- 已移动 Bob Shen 2012年5月9日 9:16 (发件人:Visual C#)