我想启动一个cmd文件并立即获得输出。
看看我的代码。指示process.WaitForExit()
不等待;为什么不?
如果我不以隐藏模式启动它,copyf.cmd
运行正常,因为显示的dosbox运行到cmd的末尾。
在隐藏模式下,cmd处于关闭状态,因为在{cmd}完成之前process.WaitForExit()
不会进行操作。
public void doSomeThing( Queue<string> output, // queue for output log
Queue<string> error // queue for error log
)
{
String com = "some params";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copyf.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
Thread thread = new Thread(new ThreadStart(() =>
{
String er;
String outp;
while (true)
{
outp = process.StandardOutput.ReadLine();
if(outp != null)
output.Enqueue("Output :" + outp + "\n");
er = process.StandardError.ReadLine();
if (er != null)
error.Enqueue("Error :" + er + "\n");
}
}));
process.Start();
thread.Start();
process.WaitForExit();
}
答案 0 :(得分:0)
您应该使用C开关启动实际的控制台命令处理器“cmd.exe”,而不是调用.CMD文件。
/C
开关将保持cmd.exe
进程,直到命令文件完成执行。 WaitForExit()
将等待CMD.exe完成执行。我曾多次使用它。为了正确操作WaitForExit,必须在StandardError.ReadToEnd()之后调用它。如described here in msdn修改后的版本:
string command = string.Format("{0}\\Make\\copyf.cmd {1}", Properties.Settings.Default.pathTo , com);
int waitTime = 60000; //1 min as example here
using (var process = new Process()
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardError = true,
FileName = "cmd.exe",
Arguments = string.Format("/C \"{0}\"", command)
}
})
{
try
{
if (!process.Start())
{
//Log some error "Failed to run command";
}
}
catch (Exception ex)
{
//Log some error "Failed to start process command";
}
process.BeginOutputReadLine();
string error = process.StandardError.ReadToEnd();
process.WaitForExit(waitTime);
if (!process.HasExited)
{
//Log something "Command: did not finish in time, terminating process"
try
{
process.Kill();
}
catch { }
}
}