//Return an item of the queue (selected by user) public ArrayList GetQueueByID(long nQueueID) { //ArrayList aTempList = new ArrayList(); int nID = ItemByQueueID(nQueueID); if (nID == -1) { return(new ArrayList()); } ArrayList aTempArray = new ArrayList(); QueueStruct aStruct = (QueueStruct)aQueue[nID]; aTempArray.Add(aStruct.sDisplayName); //0 aTempArray.Add(aStruct.sDisplayDirectory); //1 aTempArray.Add(aStruct.sDisplayPath); //2 aTempArray.Add(aStruct.sSource); //3 aTempArray.Add(aStruct.sDestination); //4 aTempArray.Add(aStruct.sHandbrakeProfile); //5 aTempArray.Add(aStruct.nStatus); //6 aTempArray.Add(aStruct.iProcentage); //7 aTempArray.Add(aStruct.sETA); //8 aTempArray.Add(aStruct.nQueueID); //9 aTempArray.Add(aStruct.aProcessOutput); //10 return(aTempArray); }
//Check if the queue has a running process, if not start the first private bool CheckNextItemInQueue() { //Idle item is set to -1 until it is executed long nFirstIdleItem = -1; for (int i = 0; i < aQueue.Count; i++) { QueueStruct sStruct = (QueueStruct)aQueue[i]; //If we found a running, then exit if (sStruct.nStatus == QueueStatus.RUNNING) { return(false); } //Set the idle item number if (sStruct.nStatus == QueueStatus.NOT_RUNNING && nFirstIdleItem == -1) { nFirstIdleItem = sStruct.nQueueID; } } //If there is an idle item, then execute it right away if (nFirstIdleItem != -1) { this.ExecuteQueue(nFirstIdleItem); return(true); } return(false); }
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; } } }
//Function that fires away a convert with handbreak private bool ExecuteQueue(long nQueueID) { int nItem = ItemByQueueID(nQueueID); if (nItem == -1) { return(false); } QueueStruct aStruct = (QueueStruct)aQueue[nItem]; //We don't run twice... if (aStruct.nStatus == QueueStatus.RUNNING) { return(false); } //Setting the parameters string baseFolder = MOTR_Settings.GetGlobalApplicationPath("tools"); string sExec = MOTR_Settings.GetExecuteToolPath("handbreak"); string sParams = "--optimize --preset \"" + aStruct.sHandbrakeProfile + "\" --input \"" + aStruct.sPath + aStruct.sSource + "\" --output \"" + aStruct.sPath + aStruct.sDestination + "\""; aStruct.dateRunning = DateTime.Now; aStruct.nStatus = QueueStatus.RUNNING; //extension = extension.Substring(extension.Length - 4); string sExtension = aStruct.sSource.Substring(aStruct.sSource.Length - 4); if (sExtension.ToUpper() == ".RAR") { sExec = MOTR_Settings.GetExecuteToolPath("unrar"); sParams = "x -y -p- " + aStruct.sPath + aStruct.sSource; } //Console.WriteLine("Exec: " + sExec + " " + sParams); //Now start the background process, if it fails change status of the queue if (!StartBackgroundProcess(sExec, sParams, aStruct.sPath, aStruct.nQueueID)) { aStruct.nStatus = QueueStatus.FINISHEDANDFAIL; aStruct.sETA = "Background could not be executed, please update tools"; } //Update the current item aQueue.RemoveAt(nItem); aQueue.Insert(nItem, aStruct); return(true); }
//Queuemanagement public void Run(long nQueueID) { int nItem = ItemByQueueID(nQueueID); if (nItem == -1) { return; } //Get struct and check status, return if running QueueStruct aStruct = (QueueStruct)aQueue[nItem]; if (aStruct.nStatus != QueueStatus.RUNNING) { this.ExecuteQueue(nQueueID); } }
//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); } } } } }
//Add an item to the queue public void Add(string sDisplayName, string sDisplayDirectory, string sDisplayPath, string _Path, string _Source, string _Destination, string _Profile, bool bAtTop = false) { QueueStruct sQueue = new QueueStruct(); sQueue.nQueueID = lQueueNumber; sQueue.sDisplayName = sDisplayName; sQueue.sDisplayDirectory = sDisplayDirectory; sQueue.sDisplayPath = sDisplayPath; sQueue.sPath = _Path; sQueue.sSource = _Source; sQueue.sDestination = _Destination; sQueue.sHandbrakeProfile = _Profile; sQueue.iProcentage = -1; sQueue.sETA = "(Waiting)"; sQueue.nStatus = QueueStatus.NOT_RUNNING; ArrayList aList = new ArrayList();; sQueue.aProcessOutput = aList; //Man må legge sammen .sPath + .sSource eller .sDestination for full path. sDisplayPath er bare visningsnavn (fullpath - drive)... //Now add the item to the list, bottom or top :) if (!bAtTop) { aQueue.Add(sQueue); } else { aQueue.Insert(0, sQueue); //Add first, nStatus sort them out :) } //Number always increase! :) lQueueNumber++; //Now check the queue CheckNextItemInQueue(); }
//QueueHandling ++++ public void QueueManangement(long nQueueID, string sCommand) { //Remove all the items that are "finished" if (sCommand.ToUpper() == "CLEAR-FINISHED") { for (int i = aQueue.Count - 1; i >= 0; i--) { QueueStruct aStructTemp = (QueueStruct)aQueue[i]; if (aStructTemp.nStatus == QueueStatus.FINISHED || aStructTemp.nStatus == QueueStatus.FINISHEDANDFAIL) { aQueue.RemoveAt(i); } } return; } //Stop all the running processes (set them into the queue again) if (sCommand.ToUpper() == "STOP-ALL-RUNNING") { ClearProcessListAndClean(); for (int i = 0; i < aQueue.Count; i++) { QueueStruct aStructTemp = (QueueStruct)aQueue[i]; if (aStructTemp.nStatus == QueueStatus.RUNNING) { aStructTemp.nStatus = QueueStatus.NOT_RUNNING; aStructTemp.iProcentage = 0; aStructTemp.sETA = ""; aQueue.RemoveAt(i); aQueue.Insert(i, aStructTemp); } } return; } if (sCommand.ToUpper() == "REMOVE-ALL") { ClearProcessListAndClean(); aQueue.Clear(); return; } //Since we need the item to move objecs, we bail out now... int nItem = ItemByQueueID(nQueueID); if (nItem == -1) { return; } //Get struct and check status, return if running QueueStruct aStruct = (QueueStruct)aQueue[nItem]; //Used in the queuemanagement-running, needs to stop one process and then handle if (sCommand.ToUpper() == "REMOVE-RUNNING" || sCommand.ToUpper() == "STOP-RUNNING") { KillProcessByQueueID(nQueueID); CheckNextItemInQueue(); //Just remove it, if the item if (sCommand.ToUpper() == "REMOVE-RUNNING") { aQueue.RemoveAt(nItem); } else { QueueStruct aStructCheck = (QueueStruct)aQueue[nItem]; aStructCheck.nStatus = QueueStatus.NOT_RUNNING; aStructCheck.iProcentage = -1; aStructCheck.sETA = "(Waiting)"; aQueue.RemoveAt(nItem); aQueue.Insert(nItem, aStructCheck); } return; } //Rest of the commands are only for non running queue items... if (aStruct.nStatus != QueueStatus.NOT_RUNNING) { return; } //Now handle the move of the item if (sCommand.ToUpper() == "REMOVE") { aQueue.RemoveAt(nItem); return; } if (sCommand.ToUpper() == "MOVE-TOP") { aQueue.RemoveAt(nItem); aQueue.Insert(0, aStruct); return; } if (sCommand.ToUpper() == "MOVE-BOTTOM") { aQueue.RemoveAt(nItem); aQueue.Insert(aQueue.Count, aStruct); return; } if (sCommand.ToUpper() == "MOVE-UP") { //Check if we are at the top if (nItem == 0) { return; } //Loop back to see if there are others that are not running, insert in front of for (int i = nItem - 1; i >= 0; i--) { QueueStruct aStructCheck = (QueueStruct)aQueue[i]; if (aStructCheck.nStatus == QueueStatus.NOT_RUNNING) { aQueue.RemoveAt(nItem); aQueue.Insert(i, aStruct); return; } } } if (sCommand.ToUpper() == "MOVE-DOWN") { //Check if we are at the top if (nItem == aQueue.Count - 1) { return; } //Loop back to see if there are others that are not running, insert in front of for (int i = nItem + 1; i < aQueue.Count; i++) { QueueStruct aStructCheck = (QueueStruct)aQueue[i]; if (aStructCheck.nStatus == QueueStatus.NOT_RUNNING) { aQueue.RemoveAt(nItem); aQueue.Insert(i, aStruct); return; } } } }