static void Main(string[] args) { string file = ""; foreach (string arg in args) file += arg + " "; file = file.Trim(); Process process = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(process.ProcessName); foreach (Process p in processes) { if (process.Id != p.Id) { try { WindowsNamedPipe pipe = new WindowsNamedPipe("hdpvrqueue", false); StreamReader reader = new StreamReader(file, Encoding.UTF8); string data = reader.ReadToEnd(); reader.Close(); pipe.Write(Encoding.UTF8.GetBytes(data)); pipe.Close(); } catch (Exception ex) { Log.WriteLine(ex.ToString()); } return; } } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormMain formmain = new FormMain(); formmain.Show(); if (file != "") { XmlDocument doc = new XmlDocument(); doc.Load(file); formmain.AddNew(doc, false); } Application.Run(); }
public FormMain() { InitializeComponent(); namedPipeThread = new Thread(new ThreadStart(delegate() { while (true) { WindowsNamedPipe pipe = new WindowsNamedPipe("hdpvrqueue", true); try { pipe._connected.WaitOne(); byte[] buffer = new byte[10240]; int count = pipe.Read(buffer); pipe.Close(); XmlDocument doc = new XmlDocument(); doc.LoadXml(Encoding.UTF8.GetString(buffer, 0, count)); this.AddNew(doc,false); this.Invoke((MethodInvoker)delegate { this.Show(); }); } catch (ThreadAbortException) { pipe.Close(); return; } catch (Exception ex) { Log.WriteLine(ex.ToString()); return; } } })); namedPipeThread.Start(); sleepThread = new Thread(new ThreadStart(delegate { while (true) { try { checkPoint = DateTime.Now; while ((DateTime.Now - checkPoint).TotalMinutes < 10) Thread.Sleep(1000); Log.WriteLine("sleep thread detected a loop!"); DateTime min = DateTime.MaxValue; lock (this.timers) { bool recording = false; foreach (RecordTimer timer in this.timers) { if (timer.recording) { recording = true; break; } if (timer.from < min) { min = timer.from; } } if (recording) continue; } if (checkBoxAutoSleep.Checked && (min - DateTime.Now).TotalMinutes > 10) { //休眠 int suspendstatus = RecordTimer.SetSuspendState(false, true, false); if (suspendstatus == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); Console.WriteLine(suspendstatus); } } catch (ThreadAbortException) { return; } } })); sleepThread.Start(); }