private void AddOutputToProcess(Process objProcess, string sData) { //Check the data for Regexp stats (procentage and time left) //Regex x = new Regex(@"[,] (.*) [%] .*[ETA ](.*)\)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); Regex x = new Regex(@"[,] (?<procentage>.*?)\.\d\d [%] .*[ETA] (?<eta>.*?)\)", RegexOptions.IgnoreCase); MatchCollection mc = x.Matches(sData); //If nothing was found, try the rar-way if (mc.Count == 0) { Regex x2 = new Regex(@"\b(?<procentage>\d+)(?:\%)$(?<eta>)", RegexOptions.IgnoreCase); mc = x2.Matches(sData); } for (int i = 0; i < aProcessRunning.Count; i++) { ProcessRunning structProcess = (ProcessRunning)aProcessRunning[i]; if (structProcess.objProcess == objProcess) { for (int o = 0; o < aQueue.Count; o++) { QueueStruct structQueue = (QueueStruct)aQueue[o]; if (structQueue.nQueueID == structProcess.nQueueID) { if (mc.Count == 0) //Handbrake output not found, just add data { structQueue.aProcessOutput.Add(sData); } else //Now we got handbreak data, parse and extract, then update if nessesary { int iOldProcentage = structQueue.iProcentage; structQueue.iProcentage = Convert.ToInt32(mc[0].Groups["procentage"].Value); structQueue.sETA = mc[0].Groups["eta"].Value; aQueue.RemoveAt(o); aQueue.Insert(o, structQueue); //If the procentage has change, then trigger en update if (iOldProcentage < structQueue.iProcentage) { QueueIDEventArg eventArg = new QueueIDEventArg(); eventArg.iProcentage = structQueue.iProcentage; eventArg.nQueueID = structQueue.nQueueID; eventArg.sETA = structQueue.sETA; this.QueueProcentageUpdate(eventArg); } } break; } } break; } } }
private bool RemoveProcessFromQueue(Process hProcess) { for (int i = 0; i < aProcessRunning.Count; i++) { ProcessRunning structProcess = (ProcessRunning)aProcessRunning[i]; if (hProcess == structProcess.objProcess) { aProcessRunning.RemoveAt(i); Console.WriteLine("Processes left: " + aProcessRunning.Count); return(true); } } return(false); }
//Remove a process based on the queueid private void KillProcessByQueueID(long nQueueID) { for (int i = 0; i < aProcessRunning.Count; i++) { ProcessRunning structProcess = (ProcessRunning)aProcessRunning[i]; if (structProcess.nQueueID == nQueueID) { structProcess.objProcess.OutputDataReceived -= CaptureOutput; structProcess.objProcess.ErrorDataReceived -= CaptureError; structProcess.objProcess.Exited -= Process_Exited; Debug.WriteLine("Killing pid: " + structProcess.objProcess.Id); structProcess.objProcess.Kill(); structProcess.objProcess.Close(); aProcessRunning.RemoveAt(i); return; } } }
//Called before exiting the class public void ClearProcessListAndClean() { for (int i = 0; i < aProcessRunning.Count; i++) { ProcessRunning structProcess = (ProcessRunning)aProcessRunning[i]; if (!structProcess.objProcess.HasExited) { structProcess.objProcess.OutputDataReceived -= CaptureOutput; structProcess.objProcess.ErrorDataReceived -= CaptureError; structProcess.objProcess.Exited -= Process_Exited; Debug.WriteLine("Killing pid: " + structProcess.objProcess.Id); structProcess.objProcess.Kill(); } structProcess.objProcess.Close(); } //Now we are finished aProcessRunning.Clear(); }
//Update the queue based on process private void SetQueueExitInformation(Process objProcess) { for (int i = 0; i < aProcessRunning.Count; i++) { ProcessRunning structProcess = (ProcessRunning)aProcessRunning[i]; if (structProcess.objProcess == objProcess) { for (int o = 0; o < aQueue.Count; o++) { QueueStruct structQueue = (QueueStruct)aQueue[o]; if (structQueue.nQueueID == structProcess.nQueueID) { structQueue.nStatus = QueueStatus.FINISHED; string sETA = "Finished " + DateTime.Now.ToString() + PrettyDurationFormat(DateTime.Now - structQueue.dateRunning); if (objProcess.ExitCode != 0) { structQueue.nStatus = QueueStatus.FINISHEDANDFAIL; sETA = "Finished, with errors " + DateTime.Now.ToString() + PrettyDurationFormat(DateTime.Now - structQueue.dateRunning);; } structQueue.sETA = sETA; aQueue.RemoveAt(o); aQueue.Insert(o, structQueue); //Update queue data everywhere QueueIDEventArg eventArg = new QueueIDEventArg(); eventArg.iProcentage = 100; eventArg.nQueueID = structQueue.nQueueID; eventArg.sETA = sETA; this.QueueProcentageUpdate(eventArg); //Last check the next item in queue, if executed update the queue CheckNextItemInQueue(); //Update the new queue this.QueueUpdate(EventArgs.Empty); } } } } }
private bool StartBackgroundProcess(string sPath, string sArgs, string sWorkingDir, long nQueueID) { ProcessStartInfo startInfo = new ProcessStartInfo(sPath, sArgs); startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.WorkingDirectory = sWorkingDir; try { Process process = new Process(); process.StartInfo = startInfo; process.EnableRaisingEvents = true; process.OutputDataReceived += CaptureOutput; //process.OutputDataReceived += (sender, e) => CaptureOutput(sender, e, this); process.ErrorDataReceived += CaptureError; //process.ErrorDataReceived += (sender, e) => CaptureError(sender, e, this); process.Exited += new EventHandler(Process_Exited); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); ProcessRunning structProcess = new ProcessRunning(); structProcess.nQueueID = nQueueID; structProcess.objProcess = process; aProcessRunning.Add(structProcess); return(true); } catch (Exception ex) { Console.WriteLine("Error background process: " + ex.Message); return(false); } }