private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { char[] delimiterChars = { '\t', '\r' }; // Collect the sort command output. if (!String.IsNullOrEmpty(outLine.Data)) { numOutputLines++; // Add the text to the collected output. //Output.Append(Environment.NewLine + Output.Append("[" + numOutputLines.ToString() + "] - " + outLine.Data); #if true // string[] strArray = outLine.Data.Split(delimiterChars); if (rcvState == enmRcvState.found) { if (strArray[1] == "NA") { rcvState = enmRcvState.NA; } if (strArray[1] != "NA") { rcvState = enmRcvState.value; ModifydBGain = strArray[2]; //ModifydBGain Debug.WriteLine(strArray[2]); } } else if (rcvState == enmRcvState.idle && strArray[0] == "File") { rcvState = enmRcvState.found; } #endif } }
private void checkMp3(string fname, string args, bool async = true) { rcvState = enmRcvState.idle; numOutputLines = 0; if (chkUsingTempfile.Checked) { if (File.Exists("tmp.mp3")) { File.Delete("tmp.mp3"); } File.Move(fname, "tmp.mp3"); //File.Copy(fname, "tmp.mp3"); } #if true int status = -1; try { String command = "\\mp3gain.exe"; Debug.Print(String.Format("Launching '{0}'", Application.StartupPath + command)); ProcessStartInfo psi = new ProcessStartInfo(Application.StartupPath + command); if (chkUsingTempfile.Checked) { psi.Arguments = " " + args + " " + "\"" + "tmp.mp3" + "\"" + ""; } else { psi.Arguments = " " + args + " " + "\"" + fname + "\"" + ""; } psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; using (Process proc = Process.Start(psi)) { List <String> errorOutput = new List <String>(); List <String> stdOutput = new List <String>(); status = GrabProcessOutput(proc, errorOutput, stdOutput, false /* waitForReaders */); } } catch (IOException ioe) { Debug.Print("Unable to run: {0}", ioe.Message); } catch (ThreadInterruptedException ie) { Debug.Print("Unable to run: {0}", ie.Message); } catch (Exception e) { Debug.Print(e.Message); } #else rcvState = enmRcvState.idle; Process pp = new Process(); pp.StartInfo.Arguments = " " + args + " " + "\"" + fname + "\"" + ""; if (chkUsingTempfile.Checked) { if (File.Exists("tmp.mp3")) { File.Delete("tmp.mp3"); } File.Move(fname, "tmp.mp3"); //File.Copy(fname, "tmp.mp3"); pp.StartInfo.Arguments = " " + args + " " + "\"" + "tmp.mp3" + "\"" + ""; } pp.StartInfo.FileName = Application.StartupPath + "\\mp3gain.exe"; //pp.StartInfo.Arguments = "/r /d 20 /k /c " + "tmp.mp3" + ""; //pp.StartInfo.Arguments = " " + args + " " + "\"" + fname + "\"" + ""; //pp.StartInfo.Arguments = " " + args + " " + "\"" + "tmp.mp3" + "\"" + ""; Debug.WriteLine(pp.StartInfo.FileName + pp.StartInfo.Arguments); pp.StartInfo.UseShellExecute = false; if (async) { pp.StartInfo.RedirectStandardOutput = true; } pp.StartInfo.CreateNoWindow = true; pp.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); pp.Start(); if (async) { pp.BeginOutputReadLine(); } pp.WaitForExit(); pp.Close(); Debug.WriteLine(Output.ToString()); textView.Text = Output.ToString(); Output.Clear(); #endif if (chkUsingTempfile.Checked) { File.Move("tmp.mp3", fname); } progressConvert.PerformStep(); }
/// <summary> /// Get the stderr/stdout outputs of a process and return when the process is done. /// Both <b>must</b> be read or the process will block on windows. /// </summary> /// <param name="process">The process to get the ouput from</param> /// <param name="errorOutput">The array to store the stderr output. cannot be null.</param> /// <param name="stdOutput">The array to store the stdout output. cannot be null.</param> /// <param name="waitforReaders">if true, this will wait for the reader threads.</param> /// <returns>the process return code.</returns> private int GrabProcessOutput(Process process, List <String> errorOutput, List <String> stdOutput, bool waitforReaders) { if (errorOutput == null) { throw new ArgumentNullException("errorOutput"); } if (stdOutput == null) { throw new ArgumentNullException("stdOutput"); } // read the lines as they come. if null is returned, it's // because the process finished Thread t1 = new Thread(new ThreadStart(delegate { // create a buffer to read the stdoutput try { using (StreamReader sr = process.StandardError) { while (!sr.EndOfStream) { String line = sr.ReadLine(); if (!String.IsNullOrEmpty(line)) { Debug.Print(line); errorOutput.Add(line); } } } } catch (Exception) { // do nothing. } })); Thread t2 = new Thread(new ThreadStart(delegate { // create a buffer to read the std output try { using (StreamReader sr = process.StandardOutput) { while (!sr.EndOfStream) { String line = sr.ReadLine(); if (!String.IsNullOrEmpty(line)) { numOutputLines++; stdOutput.Add(line); Debug.Print(line); #if true // char[] delimiterChars = { '\t', '\r' }; string[] strArray = line.Split(delimiterChars); if (rcvState == enmRcvState.found) { if (strArray[1] == "NA") { rcvState = enmRcvState.NA; } if (strArray[1] != "NA") { rcvState = enmRcvState.value; ModifydBGain = strArray[2]; //ModifydBGain Debug.WriteLine(strArray[2]); } } else if (rcvState == enmRcvState.idle && strArray[0] == "File") { rcvState = enmRcvState.found; } #endif } } } } catch (Exception) { // do nothing. } })); t1.Start(); t2.Start(); // it looks like on windows process#waitFor() can return // before the thread have filled the arrays, so we wait for both threads and the // process itself. if (waitforReaders) { try { t1.Join(); } catch (ThreadInterruptedException) { } try { t2.Join(); } catch (ThreadInterruptedException) { } } // get the return code from the process process.WaitForExit(); return(process.ExitCode); }