system.InvalidOperationException: the object is currently in use .

G

Guest

system.InvalidOperationException: the object is currently in use elsewhere...

has anyone experienced this error before? I am occasionally getting this
error. I can't figure out what could possibly cause it. When the prog runs
thru the loop, the error could be thrown anytime. Sometimes it doesn't at all.

Seems like it's telling me that the picturebox is in use at the same time
that it wants to be updated. But I timed it...
displayImg = new bitmap(.... ) takes a lot longer than
updateUI_picturebox(Picturebox1, displayImg). Updating the image should not
take much time at all. What happening.... ?

'*****************

private Delegate Sub updateUI_Picturebox_delegate(ByVal picbox As
PictureBox, ByVal imgToDisplay As Image)

private sub someSub()
for i as integer = 0 to 99 ' for example
displayImg = new bitmap(.... ) ' assign some image
updateUI_picturebox(Picturebox1, displayImg) ' display image on
picturebox
next i
end sub
'-------------------------
Private Sub updateUI_pictureBox(ByVal picbox As PictureBox, ByVal
imgToDisplay As Image)

Dim handler As New updateUI_Picturebox_delegate(AddressOf
updateUI_Picturebox_handler)
Dim args() As Object = {picbox, imgToDisplay}
Me.BeginInvoke(handler, args)

End Sub
'-------------------------
Private Sub updateUI_Picturebox_handler(ByVal picBox As PictureBox,
ByVal imgToDisplay As Image)
Try
picBox.Image = imgToDisplay ' this line throws the
exception......
Catch ex As Exception
MsgBox(ex.ToString) ' exception is caught here....
End Try
End Sub
 
G

Guest

I found that the exception was thrown because the imgToDisplay was accessed
at the same time the picBox.image was going to be updated.

So I did a work-around, as follows....
( I don't think this is the best solution, so I do NOT recommend this
solution )

Private Sub updateUI_Picturebox_handler(ByVal picBox As PictureBox,
ByVal imgToDisplay As Image)

Do

Try
picBox.Image = imgToDisplay
Exit Do
Catch es as system.invalidOperationException
GoTo continueWithLoop
Catch ex As Exception
MsgBox(ex.ToString) ' other exception is caught here
Exit Do
End Try
continueWithLoop:

Loop

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top