public void InitPipe() { try { ChildPipeClient_1 = new NamedPipeClientStream(".", "np1", PipeDirection.In); //所有管道都在使用中,父子说明重复了 ChildPipeServer_2 = new NamedPipeServerStream("np2", PipeDirection.Out); sr_1 = new StreamReader(ChildPipeClient_1); sw_2 = new StreamWriter(ChildPipeServer_2); ChildPipeClient_1.Connect(); //此处的顺序很重要,必须与服务器向匹配,否则会一直阻塞 ChildPipeServer_2.WaitForConnection(); sw_2.AutoFlush = true; //reconnecting 只有客户端 //ReConnect(); Logger2.Infor("pipe connected 02"); StartPipeThread(); } catch (Exception ex) { Logger2.Error(ex.Message); ChildPipeClient_1.Close(); ChildPipeServer_2.Close(); sr_1.Close(); sw_2.Close(); MessageBox.Show(ex.Message, "子进程"); // throw; } }
public static void Main() { //Logger2.Write("hello"); //try //{ // string a = "ff"; // int b = Convert.ToInt32(a); //} //catch (Exception ex) //{ // Logger2.Write(ex.Message); //} Stopwatch st = new Stopwatch(); st.Start(); string mg = "handshake error, plc init failed 在 System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)在 System.Net.Sockets.Socket.Connect(String host, Int32 port)在 PLCCommunicationKit.SocketBaseKit.SocketBase.initSocketBase(String ip, Int32 "; System.Threading.Tasks.Parallel.For(0, 1000, x => { Logger2.Infor("test" + x.ToString()); Logger2.Error(mg + x.ToString()); //Console.WriteLine(x); }); st.Stop(); Console.WriteLine(st.ElapsedMilliseconds); Console.ReadKey(); }
public void StartProcess(ProcessStartInfo ps) { try { Logger2.Infor("starting process"); using (Process pt = Process.Start(ps)) //using (Process progressTest = Process.Start(ps)) { // _ = progressTest.TotalProcessorTime; // 为异步获取订阅事件 pt.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived); pt.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived); // 异步获取命令行内容 pt.BeginOutputReadLine(); //Thread.Sleep(2000); //pt.WaitForExit(); //pt.Close(); } } catch (Exception ee) { throw; } }
public void initPipe() { try { var PipeServer = new NamedPipeServerStream("np1", PipeDirection.Out); var PipeChild = new NamedPipeClientStream(".", "np2", PipeDirection.In); sr = new StreamReader(PipeChild); sw = new StreamWriter(PipeServer); PipeServer.WaitForConnection(); PipeChild.Connect(); sw.AutoFlush = true; bool res = PipeServer.IsConnected; Logger2.Infor("pipe连接结果:" + res.ToString()); this.Invoke(new Action(() => { richTextBox1.AppendText(res.ToString() + " " + DateTime.Now + "\n"); })); ReciveDataAnalyze(); } catch (Exception ex) { MessageBox.Show(ex.Message, "主进程"); } }
private static void OnDataReceived(object Sender, DataReceivedEventArgs e) { if (e.Data != null) { Console.WriteLine(e.Data); Logger2.Infor(e.Data); } }
private void ReConnect() { if (ChildPipeClient_1.IsConnected != true) { ChildPipeClient_1.Connect(10000); Console.WriteLine("pipe1 before " + ChildPipeClient_1.IsConnected.ToString()); Logger2.Infor("pipe1 before " + ChildPipeClient_1.IsConnected.ToString()); } }
static void Main(string[] args) { try { Logger2.Infor("inside sub process 01"); if (args.Length > 0) { CpuType cpu = (CpuType)Convert.ToInt32(args[0]); string ip = args[1]; CreateSocketProcess cs = new CreateSocketProcess(cpu, ip); MyProxy = cs.MyPlcInstance; MyProxy.Open(); //cs.DB = Convert.ToInt32(args[2]); //cs.StartAddr = Convert.ToInt32(args[3]); //cs.ReadCount = Convert.ToInt32(args[4]); #region MyRegion //pip communicate //foreach (string s in args) //{ // Logger2.Infor("args is :" + s); //} // Console.WriteLine(args[0]); //sw.WriteLine("client recieved :"+temp); #endregion PipeHelper ph = new PipeHelper(); ph.InitPipe(); //ReadPlcAndWritePipe(cs, MyProxy); Console.ReadKey(); } else { Console.WriteLine("wrong input para number,please check"); Console.ReadKey(); } } catch (Exception ex) { Logger2.Error(ex.Message); MessageBox.Show(ex.Message, "子进程提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void StartPipeThread() { //Task ReadingTask = new Task(Pipe4Reading); //Task<bool> SendingTask = new Task(Pipe4Sending,"ss"); //Task SendingTask = new Task(() => Pipe4Sending("initing")); Thread th = new Thread(Pipe4Reading); Thread th2 = new Thread(new ParameterizedThreadStart(Pipe4Sending)); th.IsBackground = true; //th2.IsBackground = true; th.Name = "p4r"; th.Start();//接收线程 //th2.Name = "p4w"; sw_2.AutoFlush = true; sw_2.WriteLine("from sub process: sub thread status {0},{1}", th.IsAlive, th2.IsAlive); Logger2.Infor($"from sub process: sub thread status {th.IsAlive},{th2.IsAlive}"); //th2.Start("ssss"); //Console.WriteLine("start rec send thread {0} {1}",th.IsAlive,th2.IsAlive); //Pipe4Sending("ssss"); //Pipe4Sending(); }
private void button5_Click(object sender, EventArgs e) { #region test //var s = Math.Round(53.3012619227981, 2); ////Random sss = new Random(Convert.ToInt32( DateTime.Today.Day)); //double punchingRadius = 51.9290909090909; //punchingRadius = Math.Round(punchingRadius, 2); //var sss = Convert.ToDouble(punchingRadius.ToString().Split('.')[1]) / 200; //richTextBox1.Text = sss.ToString(); #endregion if (check_process_status()) { try { string temp; if ((temp = richTextBox2.Text) != "") { sw.WriteLine(temp); richTextBox2.Clear(); } else { MessageBox.Show("请先输入"); } } catch (Exception ex) { Logger2.Infor(ex.Message); MessageBox.Show(ex.Message); } } else { MessageBox.Show("请先开启子进程", "主线程提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static void exit() { Logger2.Infor("exited"); Environment.Exit(0); }
/// <summary> /// 子进程 读取管道中服务器的命令 /// </summary> public void Pipe4Reading() { try { string temp; while ((temp = sr_1.ReadLine()) != null) { try { Logger2.Infor(temp + " 03"); //string temp = "N hello"; string mode = temp.Split(' ')[0]; //string mode = temp.Substring(0, 1); string str_cmd = temp.Substring(2); sw_2.WriteLine(mode); sw_2.WriteLine(str_cmd); switch (mode) { case "R": //reading from plc { object s = Childprogram.MyProxy.Read(str_cmd); sw_2.WriteLine(s); } break; case "W": //writing to plc { sw_2.WriteLine("w"); } break; case "exit": // normal,not about plc { sw_2.WriteLine("exiting"); exit(); } break; case "hello": { sw_2.WriteLine("Hi"); } break; default: MessageBox.Show("error cmd", "子进程解析"); break; } } catch (Exception ex) { MessageBox.Show(ex.Message, "子进程"); } // Logger2.Infor("recing " + temp); } } catch (Exception ex) { MessageBox.Show(ex.Message, "子进程"); // throw; } }