简体   繁体   English

在不同的UI线程上创建的WPF访问窗口

[英]WPF Access window created on different UI thread

I created new Window on a different thread because it has heavy UI operations, that was done to keep my main window run smoothly. 我在不同的线程上创建了新的Window,因为它具有大量的UI操作,这样做是为了使主窗口保持平稳运行。 Everything works perfectly. 一切正常。

But here is the question: 但这是一个问题:
How I can access newly created window? 如何访问新创建的窗口?
After calling Dispatcher.Run() I can not manipulate visualisationWindow anymore. 调用Dispatcher.Run()我无法再操纵visualisationWindow I want to keep access to that newly created window object. 我想继续访问该新创建的窗口对象。

Here is how my window created: 这是我的窗口的创建方式:

    private void CreateVisualisationWindow()
    {
        Thread VisualisationWIndowThread = new Thread(new ThreadStart(ThreadStartingPoint));
        VisualisationWIndowThread.SetApartmentState(ApartmentState.STA);
        VisualisationWIndowThread.IsBackground = true;
        VisualisationWIndowThread.Start();
    }

    private void ThreadStartingPoint()
    {
        Visualisation visualisationWindow = new Visualisation();
        visualisationWindow.Show();
        System.Windows.Threading.Dispatcher.Run();
    }

Also I tried accessing it through System.Windows.Threading.Dispatcher.FromThread(VisualisationWIndowThread) but seems I misunderstand some core things. 我也尝试通过System.Windows.Threading.Dispatcher.FromThread(VisualisationWIndowThread)访问它,但似乎我误解了一些核心内容。

I simulated your issue using two WPF Window objects and a timer to ensure that the Second Window was created before calling operations on it. 我使用两个WPF Window对象和一个计时器模拟了您的问题,以确保在调用第二个Window之前创建了第二个Window。 Below is my code sample and it updates the second Windows TextBox every five seconds: 下面是我的代码示例,它每五秒钟更新一次第二个Windows TextBox:

    private Timer _timer;
    private SecondWindow _secondWindow;

    public MainWindow()
    {
        InitializeComponent();
        CreateVisualisationWindow();
        _timer = new Timer(Callback);
        _timer.Change(5000, 5000);
    }

    private void Callback(object state)
    {
        UpdateSecondWindowText();
    }

    private void CreateVisualisationWindow()
    {
        Thread VisualisationWIndowThread = new Thread(ThreadStartingPoint);
        VisualisationWIndowThread.SetApartmentState(ApartmentState.STA);
        VisualisationWIndowThread.IsBackground = true;
        VisualisationWIndowThread.Start();
    }

    private void ThreadStartingPoint()
    {
        _secondWindow = new SecondWindow();
        _secondWindow.SecondWindowTextBlock.Text = "Hello";
        _secondWindow.Show();
         Dispatcher.Run();
    }

    private void UpdateSecondWindowText()
    {
        _secondWindow.Dispatcher.BeginInvoke(new Action(() =>
        {
            _secondWindow.SecondWindowTextBlock.Text = _secondWindow.SecondWindowTextBlock.Text + " World";
        }));
    }

So the trick is, you need to call the Dispatcher on the second Window in order to gain access to it. 因此,诀窍是,您需要在第二个Window上调用Dispatcher才能访问它。

暂无
暂无

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

相关问题 关闭与后台工作程序WorkRunCompleted在不同线程上创建的wpf窗口 - Closing a wpf window created on a different thread from the background worker WorkRunCompleted 调用线程无法访问该对象,因为在wpf窗口中使用动画时,另一个线程拥有该对象 - The calling thread cannot access this object because a different thread owns it when using animation in wpf window 从WPF中的其他线程更新UI控件时,出现“调用线程无法访问该对象,因为其他线程拥有该对象”错误 - “The calling thread cannot access this object because a different thread owns it” error when updating UI control from different thread in WPF 如何访问和使用在WPF中的不同线程上创建的对象 - How to access & use an object which has been created on a different thread in WPF 访问XAML WPF中创建的UI元素 - Access created UI Elements in XAML WPF 不同STA线程中的WPF娱乐窗口 - WPF Recreation window in different STA thread 从其他线程更新wpf window属性 - updating a wpf window property from different thread 访问在WPF应用程序的不同Window中创建的对象 - Accessing object created in different Window in WPF application 如何从WPF中的BackgroundWorker线程直接访问UI线程? - How to directly access the UI thread from the BackgroundWorker thread in WPF? 如何在WPF应用程序中查找使用自己的UI线程运行的窗口 - How to find window running with own UI thread in WPF application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM