提问人:Tim Makins 提问时间:1/12/2023 最后编辑:Tim Makins 更新时间:1/13/2023 访问量:104
“对象当前在其他地方使用”,但它在哪里使用,谁在使用它?
"Object is currently in use elsewhere", but where is it in use, and who is using it?
问:
有很多答案描述了为什么会发生这个错误,但我找不到任何答案来解释如何确定 (a) 哪个例程失败,(b) 我们在谈论哪个对象,以及 (c) 哪个其他代码位正在使用该对象。通常我们至少会得到一个行号,但出于某种原因,这个错误想让我猜测。有什么想法吗?(这是一个运行着多个线程的大型程序。
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>MyLargeProgram.exe</AppDomain><Exception><ExceptionType>System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object is currently in use elsewhere.</Message><StackTrace> at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message&amp; m)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</StackTrace><ExceptionString>System.InvalidOperationException: Object is currently in use elsewhere.
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message&amp; m)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</ExceptionString></Exception></TraceRecord>
应用程序创建一个大图像,该图像最终显示在 PictureBox 上。最终图像由线程分 4 个阶段从其他 4 个图像创建,线程复制其中一个现有图像,对其进行更改,然后再次将其保存到该图像。如果图像在其他地方使用,则有一些标志可以阻止图像被访问。最终线程获取第 4 个图像,对其进行更改,然后将修改后的图像的克隆设置为 PictureBox 图像。
SyncLock bm
PbWM.Image?.Dispose()
PbWM.Image = CType(bm.Clone, Bitmap)
End SyncLock
我以为我已经使用了足够的 SyncLock 和标志,但也许没有。我不知道为什么System.Transactions会出现在那里。我不是在导入它。
关于锁定的另一个问题:这是我用来替换图像内容的方法。有没有更好的方法?
' The Thread enters with Bitmap bm1..
' I make a working copy of bm1
Using bm0 As Bitmap = CType(bm1.Clone, Bitmap)
Using gr As Graphics = Graphics.FromImage(bm)
' Some image drawing is done here
End Using
SyncLock bm
' Replace bm1 with the altered image
bm1 = CType(bm0.Clone, Bitmap)
End SyncLock
End Using
也许这是最好的方法?
' The Thread enters with Bitmap bm1..
' I make a working copy of bm1
Using bm0 As Bitmap = CType(bm1.Clone, Bitmap)
Using gr As Graphics = Graphics.FromImage(bm)
' Some image drawing is done here
End Using
Dim thisLock As New Object
SyncLock thisLock
' Replace bm1 with the altered image
bm1 = CType(bm0.Clone, Bitmap)
End SyncLock
End Using
还是这个?
' The Thread enters with Bitmap bm1..
' I make a working copy of bm1
Using bm0 As Bitmap = CType(bm1.Clone, Bitmap)
Using gr As Graphics = Graphics.FromImage(bm)
' Some image drawing is done here
End Using
Dim thisLock As New Object
SyncLock thisLock
' Replace bm1 with the altered image
bm1 = New Bitmap(bm0)
End SyncLock
End Using
答: 暂无答案
评论
System.Transactions
Object
Me