积极答复者
Dispatcher相同却仍出现跨线程错误

问题
-
错误提示是
必须在与 DependencyObject 相同的线程上创建 DependencySource。
我用如下代码确保线程相同
If Me.Dispatcher.Equals(MainWindowUI.Dispatcher)=True Me.ShowDialog() End If
但还是抛出了上面的异常
但奇怪的是,我把ShowDialog放在MainWindowUI的Loaded事件中执行时没有问题
-------------
以下是补充:
我又debug几次,发现问题是出在,抛出异常的Window中有一个ListBox,而这个ListBox的Item,我在Add的时候使用的是来自于其他线程的BitmapImage
所以才出现那个异常,可又如何将来自其他线程的BitmapImage合法的变为此线程的资源呢?
答案
-
好吧,我只能暂时对BitmapImage用Freeze方法了,希望以后不要有修改的代码
- 已建议为答案 Jie BaoModerator 2011年5月23日 0:58
- 已标记为答案 Jie BaoModerator 2011年5月27日 6:37
-
BitmapImage 实例默认是非线程安全的,要么你在你所调用的Thread中创建BitmapImage 实例,要么按照你的解决方法,冻结 (Freeze) BitmapImage 实例,然后就可以安全的跨线程调用了。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Jie BaoModerator 2011年5月27日 6:37
全部回复
-
好吧,我只能暂时对BitmapImage用Freeze方法了,希望以后不要有修改的代码
- 已建议为答案 Jie BaoModerator 2011年5月23日 0:58
- 已标记为答案 Jie BaoModerator 2011年5月27日 6:37
-
BitmapImage 实例默认是非线程安全的,要么你在你所调用的Thread中创建BitmapImage 实例,要么按照你的解决方法,冻结 (Freeze) BitmapImage 实例,然后就可以安全的跨线程调用了。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Jie BaoModerator 2011年5月27日 6:37