用第二个线程打开窗口



我将使用下面的代码打开我的下一个窗口,因为它正在UI线程的另一个线程上运行。我得到了一个'Initialization of 'System.Windows.Controls.X' threw an exception.'...,但当我在UI线程中调用下一个Window时,我没有收到错误。如何从另一个线程调用Window?

Page2 page2 = new Page2();
App.Current.MainWindow = page2;
this.SimpleInvoke(() => this.Close());
this.SimpleInvoke(() => page2.Show());
public static class DispatcherObjectExtensions
{
    public static void SimpleInvoke(this DispatcherObject dispatcherObject, Action action)
    {
        dispatcherObject.Dispatcher.Invoke(action);
    }
}

试试这样的东西:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,               (ThreadStart)delegate()
{
       // in the gui thread here, so do what you gotta do
});

最新更新