一个程序用到了后台调用执行Clementine模型的方法,用了new Process()的方法创建了新的进程:
string runPath = string.Format("-stream /"{0}//{1}/" -execute -log /"{0}//log.txt/" -appendlog", ServerPath, fileName);
private Process p = new Process();
p.StartInfo.FileName = "clemb.exe";//这里是设置要调用的目标程序或文件
p.StartInfo.Arguments = cmdString;
p.StartInfo.UseShellExecute = false;//是否使用操作系统外壳程序启动进程
p.StartInfo.RedirectStandardInput = true;//重定向标准输入方向
p.StartInfo.RedirectStandardOutput = true;//重定向标准输出方向
p.StartInfo.RedirectStandardError = true;//是否将进程的错误输出写入 Process 实例的
p.StartInfo.CreateNoWindow = true;//是否显示CMD命令提示符窗口 true为不显示 false为显示!
p.Start();
string mess = p.StandardOutput.ReadToEnd();//取得CMD执行结果可用于调用或其它用途
p.Close();
开始的使用使用p.StartInfo.FileName = "cmd.exe"进行调用clemb命令,结果发现一旦cmd进入后台后,运行的程序是单独进程,后来干脆用clemb进行,一个小小的问题浪费了我半天多时间。
下面是终止进程使用的代码
if (myThd.ThreadState.ToString() == "Running")//
{
p.CloseMainWindow();
p.Kill();
p.Close(); //关闭
myThd.Abort();
}