Creating a WPF element in another Thread

后端 未结 2 1109
说谎
说谎 2021-01-22 01:14

I\'m able to run 2 or more WPF windows on different thread. The problem is that now my application in splitted in many windows.

What I really want is have a main window

相关标签:
2条回答
  • 2021-01-22 01:43

    "What I really want is have a main window containg a grid in which every cell contains an element managed by a different thread."

    One way to go about this would be to create your elements normally in the cell. Create a regular class ViewModel that doesn't touch the UI but runs on it's own thread. This class is the brains behind what you're actually trying to DO with in your cells, not what you're trying to SHOW in your cells. This ViewModel class should implement INotifyPropertyChanged when it's data is has been updated. In your MainWindow.cs file you can set your cell elements' DataContext to these ViewModels. Lastly, in your XAML you can Bind things you're trying to show with the Properties in your ViewModel.

    I know I breezed over a lot of details, but it's a starting point. Lots of help to be had around here if you need any.

    0 讨论(0)
  • 2021-01-22 01:49

    It's not possible in WPF and even if it was it would have been a bad idea:

    1. It's not possible in WPF because WPF element can only be used by the thread that created them, if you add a child element from another thread they wouldn't be able to communicate.

    2. In pure Win32 it is possible - but it joins the two threads message queues so the threads are no longer independent (so even if you find a hack that makes it work with WPF it still doesn't help you)

    3. Any thread that has UI and performs a long running task can hung the entire system - so it's impotent to never perform any long running task in a UI thread - instead run the long task in a background thread

    4. because you have to keep the UI thread responsive -> it should never be busy for a noticeable length of time -> it can handle all your windows because it's not too busy.

    0 讨论(0)
提交回复
热议问题