private void UpdateProcessList(object sender, ElapsedEventArgs e) { // Update the master process dictionary from here // raised the corresponding events as necessary Process[] pList = Process.GetProcesses(); foreach (Process tempProc in pList) { if (processList.ContainsKey(tempProc.Id) == true) { ProcessStruct tempStruct = new ProcessStruct(); tempStruct.pInfo = tempProc; // Insert network info here processList.Add(tempProc.Id, tempStruct); // Process is already in here, not new // processUpdated_Handler(this, tempProc.Id); } else { // Process is new processAdded_Handler(this, tempProc.Id); } } foreach (ProcessStruct pStruct in processList.Values) { } }
public static Process Start(string fileName, string arguments, string input, Action <string> outputDelegate, Action <string> errorDelegate) { ProcessStartInfo start = new ProcessStartInfo(fileName, arguments); if (!string.IsNullOrEmpty(input)) { start.RedirectStandardInput = true; } start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.WindowStyle = ProcessWindowStyle.Hidden; start.CreateNoWindow = true; Process p = null; Process retVal = null; try { p = new Process(); start.UseShellExecute = false; p.StartInfo = start; p.Start(); ProcessStruct proc = new ProcessStruct(); proc.Reader = p.StandardOutput; proc.ProcessDelegate = outputDelegate; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessOutputReader), proc); ProcessStruct proc2 = new ProcessStruct(); proc2.Reader = p.StandardError; proc2.ProcessDelegate = errorDelegate; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessOutputReader), proc2); if (!string.IsNullOrEmpty(input)) { p.StandardInput.Write(input); p.StandardInput.Close(); } retVal = p; p = null; } finally { if (p != null) { p.Dispose(); } } return(retVal); }
static void ProcessOutputReader(object state) { ProcessStruct proc = (ProcessStruct)state; string sLine = string.Empty; do { sLine = proc.Reader.ReadLine(); if (sLine != null && proc.ProcessDelegate != null) { proc.ProcessDelegate.Invoke(sLine); } } while (sLine != null); }