static public void RemoveAlerts(ProcessDoneCallback cb) { lock (alertTable) { Process[] arp = new Process[alertTable.Keys.Count]; alertTable.Keys.CopyTo(arp, 0); foreach (Process p in arp) { alertTable[p] -= cb; } } }
static private void WorkerCompletedHandler(object sender, RunWorkerCompletedEventArgs e) { Process p = e.Result as Process; if (p != null) { // call the delegates ProcessDoneCallback cb = alertTable[p]; if (cb != null) { cb(p.ExitCode); } // remove the item from the hashtable lock (alertTable) { alertTable.Remove(p); } } }
static public void AlertWhenDone(Process p, ProcessDoneCallback cb) { // see if the process is already in the hashtable lock (alertTable) { if (alertTable.ContainsKey(p)) { alertTable[p] += cb; } else { alertTable[p] = cb; } } // create a background worker thread to watch the process BackgroundWorker bg = new BackgroundWorker(); bg.DoWork += new DoWorkEventHandler(DoWorkProc); bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompletedHandler); bg.RunWorkerAsync(p); }
/// <summary> /// starts a new process and executes a command /// </summary> /// <param name="shell">The command to be executed.</param> /// <param name="arguments">The arguments of the command.</param> /// <param name="writerCallback">The callback to process the input.</param> /// <param name="doneCallback">The callback to process the output.</param> public static void ExecuteCommands(string shell, string arguments, StreamWriterCallback writerCallback, ProcessDoneCallback doneCallback) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object data) { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = shell; start.Arguments = arguments; start.CreateNoWindow = true; start.UseShellExecute = false; start.RedirectStandardOutput = true; start.RedirectStandardInput = true; string result = string.Empty; using (Process process = Process.Start(start)) { using (StreamWriter writer = process.StandardInput) { writerCallback(writer); } using (StreamReader reader = process.StandardOutput) { result = reader.ReadToEnd(); } process.WaitForExit(); doneCallback(result, process.ExitCode); } })); }
static public void AlertWhenDone(Process p, ProcessDoneCallback cb) { // see if the process is already in the hashtable lock (alertTable) { if (alertTable.ContainsKey(p)) alertTable[p] += cb; else alertTable[p] = cb; } // create a background worker thread to watch the process BackgroundWorker bg = new BackgroundWorker(); bg.DoWork += new DoWorkEventHandler(DoWorkProc); bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompletedHandler); bg.RunWorkerAsync(p); }