首页 文章

应用程序关闭问题

提问于
浏览
0

在prism应用程序中,我创建了一个以模块开头的登录表单 . 在用户通过身份验证后,登录表单等待所有应用程序模块已初始化(如splah屏幕显示有关已加载模块的一些信息),然后启动shell . 除非用户点击登录表单的取消按钮,否则一切似乎都能正常工作 . 我想应用程序关闭,但因为登录表单在另一个线程中启动我无法使用Application.Current.Shutdown()(只能工作Enviroment.Exit(0),但我不确定这是一个正确的方法) . 我尝试从Application.Current.Dispatcher调用,但应用程序仍然运行 . 这是登录模块初始化,我 grab 应该关闭应用程序的事件:

public void Initialize()
{        
  Dispatcher.CurrentDispatcher.BeginInvoke(
    (Action) (() =>
        {
            Shell.Show();

            EventAggregator.GetEvent<LoginStatusEvent>().Publish(new LoginStatusEvent { LoginStatus = LoginViewStatus.Close });
        }));      

  ThreadStart showSplash =
    () =>
      {
        Dispatcher.CurrentDispatcher.BeginInvoke(
          (Action) (() =>
                {
                    Container.RegisterType<LoginViewModel, LoginViewModel>();
                    Container.RegisterType<LoginView, LoginView>();

                    var login = Container.Resolve<LoginView>();

                    EventAggregator.GetEvent<LoginStatusEvent>().Subscribe(e => login.Dispatcher.BeginInvoke(
                        (Action)(() =>
                            {
                                if (e.LoginStatus == LoginViewStatus.Aborted)
                                {
                                    login.Close();

                                    Application.Current.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));

                                    //Environment.Exit(0);
                                }
                                else if (e.LoginStatus == LoginViewStatus.Close)
                                {
                                    login.Close();                                          
                                }
                                else
                                {
                                    WaitForCreation.Set();
                                }
                            })));

                    login.Show();                            
                }));

        Dispatcher.Run();
      };

  WaitForCreation = new AutoResetEvent(false);

  var thread = new Thread(showSplash) {Name = "LoginView Thread", IsBackground = true};
  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();

  WaitForCreation.WaitOne();
}

任何帮助表示赞赏!

1 回答

  • 0

    为什么您的登录UI在后台线程上运行?似乎登录UI应该在GUI线程上运行 . 然后你有一个BackgroundWorker对象(比上面的代码更容易使用,更易于维护),当ProgressChanged方法回调到GUI线程时,它可以在需要更新UI的任何时候运行ProgressChanged吗?

    我经常将BackgroundWorker重载为MyDataObjectBackgroundWorker,并创建一个保存我的对象的属性 . 通过这种方式,我可以使用ProgressChanged轻松地来回传递数据(就像正在加载模块的信息一样) .

相关问题