代码之家  ›  专栏  ›  技术社区  ›  Paimiya

WPF检查不同UI线程中的窗口可用性?

  •  0
  • Paimiya  · 技术社区  · 7 年前

    我在一个新线程中创建了一个新窗口,如下所示:

    private WindowNew windowNew;
    
    private void Button_Click(object sender, RoutedEventArgs routedEventArgs)
    {
        Thread newWindowThread = new Thread(() =>
        {
            SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
            windowNew = new WindowNew();
            windowNew.Show();
            windowNew.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
            Dispatcher.Run();
        });
        newWindowThread.SetApartmentState(ApartmentState.STA);
        newWindowThread.IsBackground = true;
        newWindowThread.Start();
    }
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rekshino    7 年前
    private void Button_Click(object sender, RoutedEventArgs routedEventArgs)
    {
    if(windowNew!=null) return;
    ...
     windowNew.Closed += (s, e) => 
    {
    Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
    windowNew = null;
    }
    }