我正在使用参数启动应用程序,我想在“Startup.exe”关闭后关闭我的C#应用程序。在startup.exe关闭后,C#应用程序仍在运行,所以我的问题是为什么WaitForExit()不能为我工作?以及如何解决这个问题?
private void button4_Click(object sender, EventArgs e)
{
string arg0 = "/connect";
string arg1 = txtUsername.Text;
string arg2 = txtPassword.Text;
string genArgs = arg0+" "+arg1 +" "+ arg2;
string pathToFile = System.IO.Directory.GetCurrentDirectory()+"/Startup.exe";
Process runProg = new Process();
runProg.StartInfo.FileName = pathToFile;
runProg.StartInfo.Arguments = genArgs;
runProg.Start();
runProg.WaitForExit();
}
答案 0 :(得分:3)
您仍然在按钮处理程序中,因此您的应用程序将继续运行。如果要关闭应用程序,请尝试在语句末尾添加Environment.Exit
。
private void button4_Click(object sender, EventArgs e)
{
var arg0 = "/connect";
var arg1 = txtUsername.Text;
var arg2 = txtPassword.Text;
var genArgs = arg0+" "+arg1 +" "+ arg2;
var pathToFile = System.IO.Directory.GetCurrentDirectory()+"/Startup.exe";
var runProg = new Process();
runProg.StartInfo.FileName = pathToFile;
runProg.StartInfo.Arguments = genArgs;
runProg.Start();
Environment.Exit(0);
}
WaitForExit
将阻止,直到您生成的进程在继续之前已终止。见 - https://msdn.microsoft.com/en-us/library/fb4aw7b8(v=vs.110).aspx