启动一个新进程并杀死当前进程
我想从当前正在执行的进程A.exe 启动一个新进程B.exe。
一旦 B.exe 启动,我就想杀死 A.exe(当前正在执行的进程)。
虽然我可以启动 B.exe,但我无法关闭当前进程,即 A.exe。
我使用的代码是:
//Start the BT Setup Process
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe");
Process.Start(startInfo);
//Terminate the FSA
Process[] myProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process process in myProcess)
{
process.CloseMainWindow();
//all the windows messages has to be processed in the msg queue
//hence call to Application DoEvents forces the MSG
Application.DoEvents();
}
I want to start a new process B.exe from the current executing process A.exe.
And as soon as B.exe is launched I want to kill A.exe (the current executing process).
Though I can start B.exe I cannot close my current process i.e A.exe.
Code I use is:
//Start the BT Setup Process
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe");
Process.Start(startInfo);
//Terminate the FSA
Process[] myProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process process in myProcess)
{
process.CloseMainWindow();
//all the windows messages has to be processed in the msg queue
//hence call to Application DoEvents forces the MSG
Application.DoEvents();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么要从
B
关闭A
,而A
猫启动B
然后自行关闭?Why do you want to close
A
fromB
whileA
cat startB
and then close by itself?如果您陷入启动进程的困境,并在之后终止自己的进程,请使用
Environment.Exit(0)
,而不是Application.Exit()
。If you're falling into this quest of starting a process, and kill your own process after, use
Environment.Exit(0)
, notApplication.Exit()
.尝试 Process.Kill() 而不是 < a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx" rel="nofollow">Process.CloseMainWindow()。
Try Process.Kill() instead of Process.CloseMainWindow().
我知道这很旧,但在 .net 4.0 中你可以执行
Then Application.Exit 或其他操作
在启动仅在 Console.readline(); 上阻塞的控制台应用程序后,我使用关闭表单方法对 winforms 应用程序进行了测试;
I know this is old but in .net 4.0 you can do
Then Application.Exit or whatever
I tested with a winforms application using the close form method after launching a console app that just blocks on Console.readline();
如果您只想关闭当前进程,您应该可以调用 Application.Exit 而不是循环并关闭进程。
If you just want to close the current process you should be able to just call Application.Exit rather than looping through and closing processes.