Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FFMPEG进程太多_C#_Loops_Process_Ffmpeg - Fatal编程技术网

C# FFMPEG进程太多

C# FFMPEG进程太多,c#,loops,process,ffmpeg,C#,Loops,Process,Ffmpeg,我有一个视频文件列表,我想用ffmpeg转换它们。这是我的代码: public static void ConvertToMp3(String inputPath, String title) { String outputpath = "\"D:\\Mp3\\" + title + ".mp3\""; String _out; Process p = new Process(); p.StartInfo.Use

我有一个视频文件列表,我想用ffmpeg转换它们。这是我的代码:

    public static void ConvertToMp3(String inputPath, String title)
    {
        String outputpath = "\"D:\\Mp3\\" + title + ".mp3\"";

        String _out;
        Process p = new Process();

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.FileName = "ffmpeg";
        p.StartInfo.Arguments = " -i \"" + inputPath + "\" -vn -f mp3 -ab 192k " + outputpath;

        p.Start();
        p.StandardOutput.ReadToEnd();
        _out = p.StandardError.ReadToEnd();
        p.WaitForExit();

        if(!p.HasExited)
            p.Kill();

        Console.WriteLine(_out);   
    }

它工作得很好,但是当我在循环中调用这个函数n次时,它打开了太多的进程。我希望一次只打开一个进程,完成后,转到下一个进程。

检查进程计数,并仅在小于x(示例中为2)时执行代码如何

int进程=0;
foreach(System.Diagnostics.Process.GetProcesses()中的System.Diagnostics.Process myProc)
{
if(myProc.ProcessName==“进程名”)
进程++;
if(进程<2)
p、 Start();
}

WaitForExit
之前,添加此命令

p.Exited += (sender, e) =>
            {
               // Thread.Sleep(1000 * 60);
               // Thread thread = new Thread(() => callProcess());
               // thread.Start();                    
            };
这将在流程完成时起作用。我通常使用新线程。

p.WaitForExit()应使线程等待进程退出。ffmepg是否正在产生新的流程?从命令行运行此命令时会发生什么情况?它会等到转码完成吗?
p.Exited += (sender, e) =>
            {
               // Thread.Sleep(1000 * 60);
               // Thread thread = new Thread(() => callProcess());
               // thread.Start();                    
            };