java线程不能重复_调用线程无法访问此对象,因为另一个线程拥有它[重复]

这个问题在这里已有答案:

为什么我不能在以下代码中创建CroppedBitmap?我有一个例外:

调用线程无法访问此对象,因为其他线程拥有它 .

如果我将代码更改为

CroppedBitmap cb = new CroppedBitmap(new WriteableBitmap(bf), new Int32Rect(1, 1, 5, 5));

异常消失了吗?为什么?

代码1, 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工作:

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);

}

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值