使用lock语句 [英] Using the lock statement

查看:102
本文介绍了使用lock语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图挽救一个位图,我不断收到对象是目前在其他地方用错误。现在我知道GDI +与线程很烂,而且我应该使用锁定{} 声明,但它不工作。我的code是下面,我在做什么错误?

 位图位图=新位图();锁(位图)
{
    bitmap.Save([文件路径],ImageFormat.Png);
}


解决方案

您应该锁定其他虚拟对象:

  VAR objectForLock =新的对象()锁定(objectForLock)
{
    位图位图=新位图();
    bitmap.Save([文件路径],ImageFormat.Png);
}

不知道这是你的问题,但就是这样锁定工作于.NET。

I have a Bitmap that I'm trying to save and I keep getting the "Object is currently in use elsewhere" error. Now I know GDI+ sucks with threading and that I'm supposed to use the lock{} statement but it's not working. My code is below, what am I doing incorrectly?

Bitmap bitmap = new Bitmap();

lock (bitmap)
{
    bitmap.Save([filepath], ImageFormat.Png);
}

解决方案

You should lock other dummy object:

var objectForLock = new object()

lock (objectForLock)
{
    Bitmap bitmap = new Bitmap();
    bitmap.Save([filepath], ImageFormat.Png);
}

Not sure if that's your problem, but that's the way lock works in .Net.

这篇关于使用lock语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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