Check to see if CMD is idle when used as a process
Problem: Issue with the number of files passed to CMD utility.
Desired Solution: Way to be able to check that CMD has finished converting file before incrementing loop.
I'm running a utility in CMD using a user interface in C#. The utility converts audio files from .vce to .wav. If more that 30 files are selected the utility gets overwhelmed and stops working. How can I check to see that it has finished with one file conversion before the loop is incremented? .WaitForExit()
and .WaitForProcessIdle()
both did not work.
//arguments 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();
}
You are creating an unneeded cmd.exe process. You need to create vcecopy.exe processes and wait for them to finish as you launch them. Something along the following lines (I can't test this now, I'm coding on memory, but you should get the idea):
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
}
}
}