可靠地重新启动单实例 WPF 应用程序

发布于 2024-12-11 01:37:24 字数 383 浏览 0 评论 0原文

我想关闭当前的应用程序并重新启动。我看过很多关于此的帖子,但这些似乎都不适合我。

我已经尝试过

 System.Diagnostics.Process.Start(System.Windows.Application.ResourceAssembly.Location);
 System.Windows.Application.Current.Shutdown();

但这只会重新启动应用程序一次。如果我再次按下“重新启动”按钮(在已经重新启动的应用程序上),它只会关闭。

我还尝试启动新的 System.Diagnostics.Process 并关闭当前进程,但同样不会重新启动,它只是关闭。

如何重新启动当前的 WPF 应用程序?

I would like to have my current application close and restart. I have seen many posts on this, however none of these seem to work for me.

I have tried

 System.Diagnostics.Process.Start(System.Windows.Application.ResourceAssembly.Location);
 System.Windows.Application.Current.Shutdown();

However this only restarts the application once. If I press my 'restart' button again (on the already restarted app), it only closes.

I have also tried launching a new System.Diagnostics.Process and closing the current process, but again this does not restart, it simply closes.

How can I restart my current WPF application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

所有深爱都是秘密 2024-12-18 01:37:25

您可以创建另一个应用程序,您在退出应用程序时启动该应用程序,而该应用程序又会再次启动您的应用程序。有点像修补程序的工作方式,只是不修补任何内容。从好的方面来说,您可以在“重新启动应用程序”中有一个循环,它检查主应用程序进程的所有正在运行的进程,并且只有在它不再出现在进程中时才尝试重新启动它 - 然后您就得到了裸露的内容补丁程序的骨头也:)虽然您似乎没有重新启动应用程序的问题,因为它仍然在进程列表中 - 这是我在生产环境中执行此操作时所采用的方式,因为这为您提供了最大的帮助控制恕我直言。

编辑:

主应用程序的按钮事件处理程序(或您想要重新启动应用程序的任何地方)中的部分(在我的例子中是Process2BRestarted.exe):

    private void cmdRestart_Click(object sender, EventArgs e)
    {
        var info = new ProcessStartInfo();

        info.FileName = "ProcessReStarter";
        info.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(info);

        Application.Exit();
    }

这应该是进入您的实用程序/重新启动应用程序(此处为 ProcessReStarter.exe):

    private void MainForm_Load(object sender, EventArgs e)
    {
        // wait for main application process to end
        // really should implement some kind of error-checking/timer here also
        while (Process.GetProcessesByName("Process2BRestarted").Count() > 0) { }
        // ok, process should not be running any longer, restart it
        Process.Start("Process2BRestarted");
        // and exit the utility app
        Application.Exit();
    }

单击重新启动按钮现在将创建一个新进程 ProcessReStarter.exe,它将循环访问以下进程的进程列表:所有正在运行的进程 - 检查是否Process2BRestarted 仍在运行。如果该进程不再出现在列表中,它将启动一个新的 Process2BRestarted.exe 进程并自行退出。

You could create another application which you start when exiting your app and which in return does start your application again. Kind of like how a patcher would work, only without patching anything. On the plus side you could have a loop in that "restart-application" which checks all running processes for your main application process and only tries to re-start it once it does not appear in the process any longer - and you got the bare bones for a patcher also :) Whilst you do not seem to have a problem with restarting your application due to it still being in the processlist - it is the way I would go for when doing it in a production environment as this gives you the most control IMHO.

Edit:

That part in the button event handler (or wherever you want to restart your app with) of your main app (Process2BRestarted.exe in my case):

    private void cmdRestart_Click(object sender, EventArgs e)
    {
        var info = new ProcessStartInfo();

        info.FileName = "ProcessReStarter";
        info.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(info);

        Application.Exit();
    }

This should go into your utility/restarter application (ProcessReStarter.exe over here):

    private void MainForm_Load(object sender, EventArgs e)
    {
        // wait for main application process to end
        // really should implement some kind of error-checking/timer here also
        while (Process.GetProcessesByName("Process2BRestarted").Count() > 0) { }
        // ok, process should not be running any longer, restart it
        Process.Start("Process2BRestarted");
        // and exit the utility app
        Application.Exit();
    }

Clicking the restart button will now create a new process ProcessReStarter.exe, which will iterate through the process list of all running processes - checking whether Process2BRestarted is still running. If the process does not appear in the list (any longer) it will now start a new Process2BRestarted.exe process and exit itself.

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