簡體   English   中英

使用Sox並推送到stdout時,進程在WaitForExit上掛起

[英]Process hangs on WaitForExit when using Sox and pushing to stdout

我通過在c#程序中調用進程來使用sox。 我知道掛在WaitForExit或Start方法上非常流行,但是我無法處理它。 也許我從負責運行過程的代碼開始(這是從本主題中評分最高的答案進行復制和粘貼:

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.
        }
    }
}

如您所見,標准輸出是異步處理的,但程序仍在WaitForExit上掛起(即使超時設置為100000)。 當我在Windows中鍵入完全相同的命令時,cmd處理將花費不到一秒鍾的時間,因此操作不大。

sox --combine mix 1.wav 2.wav 3.wav -p | sox - --combine concatenate 4.wav output.wav << causing problem

我知道這是由Sox創建stdout的第一個操作引起的。 當我嘗試一些命令立即保存到文件時,沒有問題。

sox --combine mix 1.wav 2.wav 3.wav output.wav << no problem

答案在於您如何稱呼襪子。 我想你還是沒有完全獲得預期的輸出,等等。

 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();

有2個區別,一個是打開啟用引發事件以發送和接收任何輸出和輸入的功能,並且您只需要設置接收到的輸出數據,您似乎並沒有向它發送新的東西。 另外,通過偵聽出口,您可以全面處理事實。而不必掛起您的應用程序以希望聽到結束消息。

我的解決方案在這里 ,只需將cmd作為Process調用,並將路徑傳遞給可執行文件,並將sox的參數傳遞為process args。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM