无法使用与其父级Freezable属于不同线程的DependencyObject [英] Cannot use a DependencyObject that belongs to a different thread than its parent Freezable - prism

查看:120
本文介绍了无法使用与其父级Freezable属于不同线程的DependencyObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发WPF应用程序时遇到了一个问题. 该应用程序基于Prism. 该应用程序使用棱镜引导程序和 在加载任何窗口之前,该应用会在其他线程(STA)上打开一个模式对话框, 然后加载一堆东西(服务等) 该对话框在此期间处于打开状态,并允许用户通知应用程序启动过程的进度(使用事件聚合器传递更新). 加载完成后,引导程序将关闭对话框并打开主应用程序窗口. 到目前为止一切顺利... 然后,当关闭应用程序时,同样的事情在进行. 关闭主窗口,打开一个对话框(同样在新的STA线程上),以允许通知. 但是现在,当点击ShowDialog调用(发生在新的STA线程内)时, 引发异常: 不能使用与其父级Freezable属于不同线程的DependencyObject". 经过长时间的调试,我发现导致异常的原因是窗口的背景,该背景是从应用程序级别的合并字典中获取的画笔/图像(在wpf UI线程上实例化). 如果在没有ResouceDictionary的情况下加载图像-一切都会很好.

I have came across a problem while developing a WPF application. The application is based on Prism. The application boots using the prism bootstraper and before loading any Window, the app opens a modal dialog on a different thread (STA), and then a bunch of stuff are loaded (services and etc.) The dialog is open during that time and allows to notify the user on the progress of the application start-up process (using event aggregator to pass the updates). When loading is done, bootstraper closes the dialog and opens the main application window. So far so good... Then when closing the application, same thing is going on. the Main window is closed, a dialog box is opened (again on a new STA thread), to allow notifications. But now, when hitting the ShowDialog call (which occurs inside the new STA thread), an exception is raised: "Cannot use a DependencyObject that belongs to a different thread than its parent Freezable". After long long hours of debugging I have figured out the cause for the exception was the background of the window which is a brush/image taken from a merged dictionary at the application level (instantiated on the wpf UI thread). If loading the image without a ResouceDictionary - everything goes well.

摘要: 仅当使用resourceDictionary时才观察到该异常,并且仅在第二次调用新的STA线程时才观察到该异常,这反过来会加载DialogBox并在调用ShowDialog时恰好引发异常 如果只有一个对话框(例如在启动时没有对话框,而在关闭过程中只有对话框),则不会发生该异常.

To summaries: The exception is observed only when using a resourceDictionary and only on the second call to a new STA thread which in turn loads up a DialogBox and raise an exception exactly when calling ShowDialog If you have only one dialog (for example no dialog at boot time and only dialog at the shutdown process), then the exception will not occur.

然后我的问题是: 是什么原因?在这种情况下,该异常究竟意味着什么? (我知道一般来说,其他线程会进行某种UI线程更新,但是我不明白为什么仅在Dialgo +线程的第二个实例上会发生这种情况.)

My question is then: what is the reason for that? what exactly this exception mean in this case? (I understand that in general there is some kind of UI thread updates form other threads, but then I do not understand why this happens only on the second instance of the dialgo+thread).

谢谢:)

推荐答案

正如您正确提到的,您的背景对象是在主UI线程上创建的.您的背景实际上是一个Brush对象,而Brush是一个DependencyObject.

As you correctly mentioned, your background object was created on main UI thread. Your background is actually a Brush object and Brush is a DependencyObject.

创建DependencyObject时,它依赖"它所基于的STA线程.因此,像其他依赖对象一样,它只能在其自己的线程上使用.这意味着STA与旧的COM对象模型具有某种兼容性.

When a DependencyObject is created it "depends" on the STA thread it was created on. So like other dependency objects it can only be used on its own thread. This means that STA and kind of compatibility to old COM objects model.

因此,当您尝试在其他STA线程上使用它时,会得到适当的异常.

So when you try use it on the other STA thread you get an appropriate exception.

P.S对于定义为资源的图像,我也有同样的问题.

P.S I have the same problem with images that have been defined as resources.

这篇关于无法使用与其父级Freezable属于不同线程的DependencyObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆