启动一个新进程并杀死当前进程

发布于 2024-10-04 01:43:33 字数 630 浏览 2 评论 0原文

我想从当前正在执行的进程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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吻风 2024-10-11 01:43:33

为什么要从B关闭A,而A猫启动B然后自行关闭?

Process.Start("A.exe");
Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else

Why do you want to close A from B while A cat start B and then close by itself?

Process.Start("A.exe");
Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else
弄潮 2024-10-11 01:43:33

如果您陷入启动进程的困境,并在之后终止自己的进程,请使用 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), not Application.Exit().

不喜欢何必死缠烂打 2024-10-11 01:43:33

尝试 Process.Kill() 而不是 < a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx" rel="nofollow">Process.CloseMainWindow()。

千纸鹤 2024-10-11 01:43:33

我知道这很旧,但在 .net 4.0 中你可以执行

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe");
startInfo.UseShellExecute = true;//This should not block your program
Process.Start(startInfo);

Then Application.Exit 或其他操作
在启动仅在 Console.readline(); 上阻塞的控制台应用程序后,我使用关闭表单方法对 winforms 应用程序进行了测试;

I know this is old but in .net 4.0 you can do

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe");
startInfo.UseShellExecute = true;//This should not block your program
Process.Start(startInfo);

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();

最美不过初阳 2024-10-11 01:43:33

如果您只想关闭当前进程,您应该可以调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文