简体   繁体   English

在第一个关闭后,如何声明第二个新的Application对象?

[英]How can I declare a second, new Application object after the first has shut down?

This question relates to restarting an application. 此问题与重新启动应用程序有关。

I have seen this question. 我已经看到了这个问题。 However, the question, and as far as I have been able to tell, the answers, are almost a decade old, and none of them seem to provide an answer to this specific question ( of how to define second Application object once the first has had its .Shutdown method called ). 但是,这个问题(据我所知)已经有近十年的历史了,而且似乎都没有一个答案可以回答这个特定的问题(关于如何定义第二个Application对象的问题)其.Shutdown方法称为)。

Attempting to declare new App( ) after the first has had its .Shutdown( ) method called results in the following exception: 尝试在第一个具有其.Shutdown( )方法调用的new App( )后声明以下异常:

在此处输入图片说明

Is there anything that I can do with the original App object to prevent his from happening? 我可以用原始的App对象做任何事情来防止他发生吗?

In compliance with the Minimal, Complete and Verifiable Example requirements: 符合最小,完整和可验证示例的要求:

MAKE CERTAIN THAT App.xaml HAS ITS BUILD ACTION SET TO Page . 确定App.xaml构建操作已设置到Page

App.xaml.cs: App.xaml.cs:

using System;

namespace MCVE {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
    public partial class App {
        private static bool _isRestarting = true;
        public static bool IsRestarting {
            get => _isRestarting;
            set => _isRestarting = value;
        }

        [STAThread]
        public static void Main( ) {
            while ( IsRestarting ) {
                IsRestarting = false;
                App program = new App( );
                program.InitializeComponent( );
                program.Run( );
            }
        }
    }   
}

MainWindow.xaml: MainWindow.xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="MCVE.MainWindow"
    mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
    <UniformGrid Rows="1">
        <Button Content="Restart" Click="Restart" />
        <Button Content="Exit" Click="Shutdown" />
    </UniformGrid>
</Window>

MainWindow.Xaml.cs: MainWindow.Xaml.cs:

using System.Windows;

namespace MCVE {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow( ) {
            InitializeComponent( );
        }

        private void Restart( object sender, RoutedEventArgs e ) {
            App.IsRestarting = true;
            Shutdown( sender, e );
        }

        private void Shutdown( object sender, RoutedEventArgs e ) =>
            Application.Current?.Shutdown( );
    }
}

To Reproduce: Click the button that says Restart . 重现:单击显示Restart的按钮。

The Application class is designed to only run once and be associated with the lifetime of the AppDomain it is loaded into. Application类被设计为只能运行一次,并与其加载到的AppDomain的生存期相关联。 Even if you could find some way to work around that, it would be going against the intentions of how the class should be used. 即使您找到解决该问题的方法,也将与使用该类的意图背道而驰。 (See the remarks section here .) (请参阅此处的备注部分。)

Rather than fighting with the established intention of a specific class, I recommend taking a step back and thinking about what you are trying to accomplish. 我建议不要退后一步,而是退后一步,思考您要达到的目标。 If you want to restart the entire application, consider starting a new process. 如果要重新启动整个应用程序,请考虑启动一个新进程。 If you want to unload and then reload a bunch of things, build that functionality into your code at the appropriate level within the scope of a single Application instance. 如果要卸载然后重新加载一堆东西,请在单个Application实例范围内的适当级别将该功能构建到代码中。

Depending what you are trying to accomplish, you may also want to consider reading up on AppDomain (can start here , but there are probably better resources if you search around). 根据您要完成的工作,您可能还需要考虑阅读AppDomain (可以从此处开始,但是如果您四处搜索,可能会有更好的资源)。 An AppDomain is specifically designed to be an encapsulated standalone arena where you can load and unload a bunch of stuff by loading and unloading the whole domain. AppDomain专门设计为一个封装的独立竞技场,您可以在其中通过加载和卸载整个域来加载和卸载一堆东西。

I believe it is also possible to run an Application instance in each domain, but I have never tried anything like that. 我相信也可以在每个域中运行一个Application实例,但是我从未尝试过类似的方法。 If you do something like this, you should probably avoid running an Application instance in the default domain since that domain can never be unloaded except by shutting down the process. 如果执行这样的操作,则可能应避免在默认域中运行Application实例,因为除非关闭进程,否则永远无法卸载该域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM