none
Exception that I have not seen before RRS feed

  • Question

  • Here is the code:

            For ypos As Integer = 0 To MyBitmap2.Height - 1
                For xpos As Integer = 0 To MyBitmap2.Width - 1
                    CurrentColor = MyBitmap2.GetPixel(xpos, ypos)
                    CharToAdd = GetCharFromColor(CurrentColor)
                    If CharToAdd <> -1 Then
                        ReturnString.Append(Chr(CharToAdd))
                    End If
                Next
            Next

    I tried moving this code to a Background Worker but it throws an exception at the GetPixel line - "Object is in use elsewhere". The same code in the main thread is fine but of course it makes the form unresponsive. Any ideas ? I also tried putting the Function GetCharFromColor inline with no change.

    Edit: Here is some additional information:

    Exception: {"Object is currently in use elsewhere."}
    HResult: -2146233079
    Inner Exception: Nothing
    Source: System.Drawing
    Stack Trace:
       at System.Drawing.Image.get_Height()
       at System.Drawing.Bitmap.GetPixel(Int32 x, Int32 y)
       at ColorCodeEncryption.Form1.BGWDecrypt_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\fdeutsch\Documents\Visual Studio 2010\Projects\ColorEncrypt\ColorCodeEncryption V9-BGW\ColorCodeEncryption\Form1.vb:line 359
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

    • Edited by Devon_Nullman Wednesday, September 27, 2017 12:33 PM Added Information
    Wednesday, September 27, 2017 12:09 PM

Answers

  • I tried moving this code to a Background Worker but it throws an exception at the GetPixel line - "Object is in use elsewhere".

    GDI can be very fussy about cross-thread issues, and sometimes throws up errors that seem to be purely GDI-created.  In this case it is telling you that the thread should be working on a clone of the bitmap instead of the one that is in use in the main thread.

    • Marked as answer by Devon_Nullman Wednesday, September 27, 2017 12:35 PM
    Wednesday, September 27, 2017 12:16 PM

All replies

  • I tried moving this code to a Background Worker but it throws an exception at the GetPixel line - "Object is in use elsewhere".

    GDI can be very fussy about cross-thread issues, and sometimes throws up errors that seem to be purely GDI-created.  In this case it is telling you that the thread should be working on a clone of the bitmap instead of the one that is in use in the main thread.

    • Marked as answer by Devon_Nullman Wednesday, September 27, 2017 12:35 PM
    Wednesday, September 27, 2017 12:16 PM
  • The only "use" in the main thread is to set a picturebox.image in another form but that did the trick - Thanks


    Wednesday, September 27, 2017 12:48 PM