I am trying to create a WPF user control which takes too long time to initiate. Therefore, I would like to create the user control in separate STA thread and add it inside a StackPanel in the main UI thread. But, it doesn't allow since the user control created in separate thread. appreciate expert's help!. Thanks.
Asked
Active
Viewed 1,216 times
1
-
Check [this question](http://stackoverflow.com/questions/10029109/running-a-wpf-control-in-another-thread) – dymanoid Feb 26 '15 at 14:30
-
http://stackoverflow.com/questions/12387687/is-it-possible-to-initialize-wpf-usercontrols-in-different-threads. check – Ayyappan Subramanian Feb 26 '15 at 14:31
-
1It can be done (see other links), but if at all possible you would be better off if you didn't have to. Do you know what is making it slow? Often (especially if it is connected to external data, or if there is some sort of expensive calculation, for example), you can decouple the slow part from the UI itself, and run the slow part in a separate thread. Bear in mind that using a thread in this way won't make it faster (unless you use multiple threads on a multi-core machine), but it will keep the UI responsive while the calculation occurs. – JMarsch Feb 26 '15 at 14:36
-
In my case, the above mentioned WPF user control is loading a third party video plugin and no more control over third party plugin. In fact, that video plugin takes too long time to initiate. If I use HostVisual technique, I could only able to display the control but it doesn't allow to access any sub controls. – Saruhasan Feb 26 '15 at 15:06
1 Answers
0
Since the custom control is doing the process, do the process on a separate thread. Create a an internal INotifyProperty property as a visibility flag which starts off is not visible. When the process is done have it change the flag and the control becomes visible within the stackpanel and all is good.
That is the standard practice for handling long running operations, away from the gui and not having to deal with STA threading issues.

ΩmegaMan
- 29,542
- 12
- 100
- 122
-
1Thanks for your reply. Custom control is taking too long time to initiate itself and it is a third party control. So, I have to initiate the control in separate STA thread. As you said, we could use separate non STA thread for long running process but not for initiating UI elements. Appreciate your help! – Saruhasan Feb 27 '15 at 01:35