I am using sox by calling process in c# program. I know that hanging on WaitForExit or Start methods is quite popular, but I cannot deal with it. Maybe I start from code reponsible for running process (which is copy&paste from best rated answer in this topic:
using (Process process = new Process())
{
process.StartInfo.FileName = _soxExePath;
process.StartInfo.Arguments = _task.Arguments.RemoveFirstOccurence("sox");
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{
result = process.ExitCode == 0;
}
else
{
// Timed out.
}
}
}
As you can see standard output is handled asynchronously but still program hangs on WaitForExit (even if timeout is set to 100000). When I type exactly the same command in Windows cmd processing takes less than a second so it isn't big operation.
sox --combine mix 1.wav 2.wav 3.wav -p | sox - --combine concatenate 4.wav output.wav << causing problem
I know it is caused by that Sox is creating first operation to stdout. When I try some command which save to file immediately there is no problem.
sox --combine mix 1.wav 2.wav 3.wav output.wav << no problem