STA线程没有加载主窗口在WPF线程、加载、窗口、STA

2023-09-06 15:59:36 作者:2013最爱你的人是我

我在App_Startup加载主窗口()。我想显示进度条,同时加载窗口。但它不工作:

 无效App_Startup(对象发件人,StartupEventArgs E)
{

    螺纹bootStrapThread =新主题(新的ThreadStart(runBootStrapProcess));
    bootStrapThread.SetApartmentState(ApartmentState.STA);
    bootStrapThread.IsBackground = TRUE;
    bootStrapThread.Start();
    _loadingProgressBar =新loadingProgressBar();
    _loadingProgressBar.ShowDialog();

}
 

我想从线程加载窗口:

 无效runBootStrapProcess()
        {
            MetadataReader MR =新MetadataReader();
            如果(currentVersionNo.Equals(remoteVersionNo))
            {
                Application.Current.Shutdown();
            }
            其他
            {
                主窗口MW =新的主窗口();
                mw.Show();
            }

            _loadingProgressBar.ShouldCloseNow = TRUE;

        }
 

解决方案 vb如何将子窗口加载到主窗口

您可以试试这个:

 无效runBootStrapProcess(){
  MetadataReader MR =新MetadataReader();
  如果(currentVersionNo.Equals(remoteVersionNo)){
    Application.Current.Shutdown();
  } 其他 {
    System.Windows.Application.Current.Dispatcher.BeginInvoke(
    新的行动(
      ()=> {
        主窗口MW =新的主窗口();
        mw.Show();
      }));
  }
  _loadingProgressBar.ShouldCloseNow = TRUE;
}
 

您基本上从线程当你想显示窗口发送到主应用程序线程。因此,这会阻止逼抢的应用程序,当线程退出,因为主窗口从主线程所示。

I am loading MainWindow in App_Startup (). I wanted to show the progress bar while loading the window. But it is not working :

void App_Startup(object sender, StartupEventArgs e)
{

    Thread bootStrapThread = new Thread(new ThreadStart(runBootStrapProcess));
    bootStrapThread.SetApartmentState(ApartmentState.STA);
    bootStrapThread.IsBackground = true;
    bootStrapThread.Start();
    _loadingProgressBar = new loadingProgressBar();
    _loadingProgressBar.ShowDialog();

}

I want to load the window from thread :

void runBootStrapProcess()
        {
            MetadataReader mr = new MetadataReader();  
            if (currentVersionNo.Equals(remoteVersionNo))
            {
                Application.Current.Shutdown();
            }
            else
            {
                MainWindow mw = new MainWindow();
                mw.Show();
            }

            _loadingProgressBar.ShouldCloseNow = true;

        }

解决方案

You can try this:

void runBootStrapProcess() {
  MetadataReader mr = new MetadataReader();
  if (currentVersionNo.Equals(remoteVersionNo)) {
    Application.Current.Shutdown();
  } else {
    System.Windows.Application.Current.Dispatcher.BeginInvoke(
    new Action(
      () => {
        MainWindow mw = new MainWindow();
        mw.Show();
      }));
  }
  _loadingProgressBar.ShouldCloseNow = true;
}

You basically from the thread when you want to show the window send it to the main application thread. This thus stops the application from closing down when the thread exits since the MainWindow is Shown from the main thread.