0

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
Community
  • 1
  • 1
Lukas
  • 621
  • 2
  • 12
  • 29

2 Answers2

0

The answer lies in how you're calling sox. Im going to guess you're not entirely getting the output you expected etc either.

 process.StartInfo.FileName = _soxExePath;
 process.StartInfo.Arguments = _task.Arguments.RemoveFirstOccurence("sox");
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.RedirectStandardOutput = true;
 process.StartInfo.RedirectStandardError = true;
 process.EnableRaisingEvents = true;
  //<set event handlers here>
 process.Exited += // define what to do to clear up
 process.Start();

Theres 2 differences, one, turning on the enabling of raising events to send and receive any output and input, and you should only need to set the output data received, you dont seem to be sending it new stuff. Also, by listening for the exit, you are able to handle the fact its all over.. rather than hang your app hoping to hear it finished..

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

My solution is here , just invoking cmd as Process and passing path to executables and sox's arguments as process args.

Community
  • 1
  • 1
Lukas
  • 621
  • 2
  • 12
  • 29