C# 检查CMD用作进程时是否处于空闲状态

C# 检查CMD用作进程时是否处于空闲状态,c#,cmd,process,C#,Cmd,Process,问题:传递给CMD实用程序的文件数有问题 理想的解决方案:一种能够在递增循环之前检查CMD是否已完成文件转换的方法 我正在CMD中使用C中的用户界面运行一个实用程序。该实用程序将音频文件从.vce转换为.wav。如果选择的文件超过30个,该实用程序将无法正常工作。在循环递增之前,如何检查是否已完成一个文件转换。WaitForExit和.WaitForProcessIdle都不起作用 //arguements are a list of files selected by the user for

问题:传递给CMD实用程序的文件数有问题

理想的解决方案:一种能够在递增循环之前检查CMD是否已完成文件转换的方法

我正在CMD中使用C中的用户界面运行一个实用程序。该实用程序将音频文件从.vce转换为.wav。如果选择的文件超过30个,该实用程序将无法正常工作。在循环递增之前,如何检查是否已完成一个文件转换。WaitForExit和.WaitForProcessIdle都不起作用

//arguements are a list of files selected by the user for conversion, 
//the folder to save the converted files in, and the path that the 
//current files are under
public static void convertVCE(List<string> files, string newPath, string filePath)
{
    Process process1 = new Process();
    process1.StartInfo.FileName = "cmd.exe";
    process1.StartInfo.CreateNoWindow = false;
    process1.StartInfo.RedirectStandardInput = true;
    process1.StartInfo.RedirectStandardOutput = true;
    process1.StartInfo.UseShellExecute = false;
    process1.Start();
    //move to directory where the utility is
    process1.StandardInput.WriteLine("cd \\Program Files (x86)\\NMS Utilities");
    process1.StandardInput.Flush();

    //loop to convert each selected file
    for (int i = 0; i < files.Count; i++)
    {
        if (files[i].EndsWith(".vce"))
        {
            string fileName = Path.Combine(filePath, files[i]);
            string newFileName = Path.Combine(newPath, files[i]).Replace(".vce", "");

            process1.StandardInput.WriteLine(string.Format("vcecopy.exe {0} {1}.wav", fileName, newFileName));
            process1.StandardInput.Flush();
        }
    }
    process1.StandardInput.Close();
    Console.WriteLine(process1.StandardOutput.ReadToEnd());
    process1.WaitForExit();
}

您正在创建一个不需要的cmd.exe进程。您需要创建vcecopy.exe进程,并在启动它们时等待它们完成。下面是一些我现在无法测试的东西,我在内存上编码,但是你应该知道:

var vceCopyStartInfo = new StartInfo();
vceCopyStartInfo.CreateNoWindow = true;
vceCopyStartInfo.UseShellExecute = false;
vceCopyStartInfo.WorkingDirectory = "\\Program Files (x86)\\NMS Utilities";
vceCopyStartInfo.FileName = "vcecopy.exe";

for (int i = 0; i < files.Count; i++)
{
    if (files[i].EndsWith(".vce"))
    {
        string fileName = Path.Combine(filePath, files[i]);
        string newFileName = Path.Combine(newPath, files[i]).Replace(".vce", "");
        //some applications need arguments in quotes, try it if this doesn't work.
        vceCopyStartInfo.Arguments = string.Format("{0} {1}.wav", fileName, newFileName));

        using (var vceCopyProcess = Process.Start(vceCopyStartInfo))
        {
            vceCopyProcess.WaitForExit();
            //if vcecopy doesn't exit by itself when it finishes, try WaitForProcessIdle
        }
    }
}

为什么要启动cmd.exe进程?启动vcecopy进程并等待该进程空闲。是否可以使用process1.StandardOutput.ReadToEnd;并将其用作完成指示器?根据您的描述,听起来vcecopy.exe不是控制台应用程序-否则cmd.exe将等待它完成。您是否考虑过使用start/wait等待vcecopy.exe的实例退出,然后再启动下一个实例。是的,您一次创建一个vcecopy.exe进程,然后等待它完成,然后再启动下一个进程。这就是你想要的,不是吗?我不理解您对文件名的问题,只需通过de ProcessStartInfo.arguments属性在每次启动时定义相应的参数。什么窗口?如果vcecopy不需要任何窗口,则不需要打开它们。您正在创建的唯一窗口是cmd.exe,您不需要它,您可以在不使用cmd.exe的情况下启动所有vcecopy执行,如果应用程序可以无窗口运行,则无需单个窗口。这太棒了!我已经按照你的建议做了一些,但WaitForExit才是关键。谢谢你的帮助。这在大量文件上非常有效。