/// <summary> /// get first line and run it /// </summary> /// <param name="main"></param> /// <param name="util"></param> /// <param name="token"></param> public void MakeTaskFromLine(MainForm main, Utilities util, CancellationToken token, String line) { // get the first word in the line (should be name of file that will be executed always) cmdFilename = util.getFileName(line); // get everything but the first word in line arguments = util.getArguments(line); // create a new process object "cmd" cmd = new System.Diagnostics.Process(); // pass the fileName to be executed cmd.StartInfo.FileName = cmdFilename; // pass the arguments for the file cmd.StartInfo.Arguments = arguments; // Setting this property to false enables you to redirect input, output, and error streams. cmd.StartInfo.UseShellExecute = false; // dont create a cmd window cmd.StartInfo.CreateNoWindow = true; // enable redirecting of output from cmd to our textbox cmd.StartInfo.RedirectStandardOutput = true; // enable redirecting of input to the cmd cmd.StartInfo.RedirectStandardInput = true; // enable redirecting of error output cmd.StartInfo.RedirectStandardError = true; // start the process cmd.Start(); try { // create a new task for output, because ffmpeg prints to error stream, account for that, else print normally Task task_output = new Task(() => { if (cmdFilename.Contains("ffmpeg")) Stderr(main, token, cmd, stderr); else { Stdout(main, token, cmd, stdout); Stderr(main, token, cmd, stderr); } }, token, TaskCreationOptions.AttachedToParent); task_output.Start(); } catch (Exception e) { textOutput.AppendText(DateTime.Now.ToString("HH:mm:ss tt") + " - Output Thread Error: " + e.Message + "\n"); } }
public string GetOutputPathJoined(MainForm main) { // put all text from input box to array main.cmdTextInput.Text.ToArray(); // loop through the array of lines foreach (String line in main.cmdTextInput.Lines) { // put all the words in line into array String[] words = line.Split('"'); // if mencoder is in line, return the third word, which should be joined filename if (line.Contains("mencoder")) { return words[1]; } } return ""; }
/// <summary> /// This function will always return the last word from the ffmpeg line, should be a path to output /// </summary> /// <param name="main"></param> /// <returns></returns> public string GetOutputPathEncode(MainForm main) { // put all text from input box to array main.cmdTextInput.Text.ToArray(); // loop through the array of lines foreach (String line in main.cmdTextInput.Lines) { // put all the words in line into array String[] words = line.Split('"'); // if ffmpeg is in line, return last word if (line.Contains("ffmpeg")) { return words[3]; } } return ""; }
public void SetOutputPath(MainForm main, String path) { main.cmdTextInput.Text = main.cmdTextInput.Text.Replace(GetOutputPathEncode(main), path); path = path.Replace("mp4", "avi"); main.cmdTextInput.Text = main.cmdTextInput.Text.Replace(GetOutputPathJoined(main), path); }
/// <summary> /// read stdout and output to textbox /// </summary> /// <param name="main"></param> /// <param name="token"></param> /// <param name="cmd"></param> /// <param name="stdout"></param> public void Stdout(MainForm main, CancellationToken token, Process cmd, StreamReader stdout) { try { // create a new streamreader that will read the cmd output stdout = cmd.StandardOutput; // output will be saved here String stdoutText = ""; // continue reading the stream until it reaches the end while (!stdout.EndOfStream) { if (token.IsCancellationRequested) { // tidy up and release resources throw new OperationCanceledException(token); } else { // stdout output goes to textbox stdoutText = stdout.ReadLine(); main.textOutput.AppendText(stdoutText + "\r\n"); } } } catch (Exception e) { main.textOutput.AppendText(DateTime.Now.ToString("HH:mm:ss tt") + " - stdout Error: " + e.Message + "\n"); } finally { stdout.Dispose(); } }
/// <summary> /// run cmd, the commands will be executed in sequence, first line will execute and program will wait until its complete, then second line etc.. /// </summary> public void runCmd(MainForm main, CancellationToken token) { // objects Utilities util = new Utilities(); try { // store all the lines in textbox into cmdTextInput.Lines[] array main.cmdTextInput.Lines.ToArray(); // for each line in text input, a task will be created, a while loop ensures that a task has to be completed before next one begins foreach (String line in main.cmdTextInput.Lines) { Task task_cmd = Task.Factory.StartNew(delegate { MakeTaskFromLine(main, util, token, line); }, token); while (!task_cmd.IsCompleted) { Thread.Sleep(10); } main.textOutput.AppendText(DateTime.Now.ToString("HH:mm:ss tt") + " - Done.\n"); } } // end try catch (Exception e) { main.textOutput.AppendText(DateTime.Now.ToString("HH:mm:ss tt") + " - CMD Error: " + e.Message + "\n"); } }