The calling thread cannot access this object because a different thread owns it(调用线程无法访问此对象,因为不同的线程拥有它)
问题描述
为什么我不能在下面的代码中创建 CroppedBitmap?我遇到了一个例外:
Why I can't create CroppedBitmap in the following code? I got an exception:
调用线程无法访问此对象,因为另一个线程拥有它.
The calling thread cannot access this object because a different thread owns it.
如果我将代码更改为
CroppedBitmap cb = new CroppedBitmap(new WriteableBitmap(bf), new Int32Rect(1, 1, 5, 5));
异常消失了吗?为什么?
the exception is gone? why ?
代码 1,cb.Freeze()
处的异常:
Code 1, an exception at cb.Freeze()
:
public MainWindow()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem((o) =>
{
//load a large image file
var bf = BitmapFrame.Create(
new Uri("D:\1172735642.jpg"),
BitmapCreateOptions.None,
BitmapCacheOption.None);
bf.Freeze();
Dispatcher.BeginInvoke(
new Action(() =>
{
CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));
cb.Freeze();
//set Image's source to cb....
}),
DispatcherPriority.ApplicationIdle);
}
);
}
代码 2,有效:
ThreadPool.QueueUserWorkItem((o) =>
{
var bf = BitmapFrame.Create(
new Uri("D:\1172740755.jpg"),
BitmapCreateOptions.None,
//BitmapCreateOptions.DelayCreation,
BitmapCacheOption.None);
bf.Freeze();
var wb = new WriteableBitmap(bf);
wb.Freeze();
this.Dispatcher.Invoke(
new Action(() =>
{
var r = new Int32Rect(1, 1, 5, 5);
CroppedBitmap cb = new CroppedBitmap(wb, r);
cb.Freeze();
//set Image's source to cb....
Image.Source = cb;
}),
DispatcherPriority.ApplicationIdle);
}
);
代码 3,无需 WritableBitmap 即可工作:
Code 3, works without WritableBitmap:
ThreadPool.QueueUserWorkItem((o) =>
{
var bf = BitmapFrame.Create(
new Uri("D:\1172735642.jpg"),
BitmapCreateOptions.None,
//BitmapCreateOptions.DelayCreation,
BitmapCacheOption.None);
bf.Freeze();
var bf2 = BitmapFrame.Create(bf);
bf2.Freeze();
this.Dispatcher.Invoke(
new Action(() =>
{
var r = new Int32Rect(1, 1, 5, 5);
BitmapSource cb = new CroppedBitmap(bf2, r);
cb.Freeze();
//set Image's source to cb....
Image.Source = cb;
}),
DispatcherPriority.ApplicationIdle);
}
);
推荐答案
您可以在反射器中查看这些类.cb.Freeze() 中会出现异常.在
You can look through this classes in reflector. Exception will rise in cb.Freeze(). In
CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));
case 构造函数做了这样的事情:
case constructor did something like this:
this.this.Source = source;
所以源不是在当前线程中创建的,所以会出现异常.在
So source wasn't created in current thread, and so exception will rise. In
new WriteableBitmap(bf)
案例,构造函数与 bf 对象同步,并且在当前线程中创建了新源,因此不会出现异常.如果您对 In Depth 细节感兴趣,您可以随时使用 Reflector 反射基础库 :)
case, constructor synchronize with bf object and new source is created in current thread, so, no exceptions will rise. If you are interested in In Depth details, you can always reflect base libraries with Reflector :)
这篇关于调用线程无法访问此对象,因为不同的线程拥有它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用线程无法访问此对象,因为不同的线程拥有


基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01