Getting an exception using a background worker

Devon Nullman 20 Reputation points
2024-08-31T18:03:29.61+00:00

Source and Destination are both bitmaps that are the same size

hgt and wid are the height and width of the images minus 1

Here's the background worker Code:

`Private Sub BGWProcess_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGWProcess.DoWork`

For Y = 0 To hgt

For X = 0 To wid

If BGWProcess.CancellationPending Then Exit Sub

Destination.SetPixel(X, Y, Source.GetPixel(X, Y)) 'Here is where it fails

Next

BGWProcess.ReportProgress(Y)

Next

End Sub

Private Sub BGWProcess_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGWProcess.ProgressChanged

TxtProgress.Text = e.ProgressPercentage.ToString

PictureBox1.Image = Destination

End Sub

The exception is "System.InvalidOperationException: Object is currently in use elsewhere."

at the line "Destination.SetPixel(X, Y, Source.GetPixel(X, Y))

There is nothing else going on that might be accessing any of the bitmaps or the PictureBox. Any ideas would be greatly appreciated.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,715 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 116.8K Reputation points
    2024-08-31T18:54:36.8233333+00:00

    Try to edit the ProgressChanged handler and also process the RunWorkerCompleted event:

    Private Sub BGWProcess_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGWProcess.ProgressChanged
        TxtProgress.Text = e.ProgressPercentage.ToString
        'PictureBox1.Image = Destination ' <-- REMOVED
    End Sub
    
    Private Sub BGWProcess_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGWProcess.RunWorkerCompleted
        PictureBox1.Image = Destination
    End Sub
    

    In addition, before starting the worker, make sure that Destination is not assigned to picture box. For example:

    PictureBox1.Image = Nothing
    BGWProcess.RunWorkerAsync()
    

  2. Jiachen Li-MSFT 31,011 Reputation points Microsoft Vendor
    2024-09-02T10:43:23.9966667+00:00

    Hi @Devon Nullman ,

    Even though you're not explicitly using the bitmap elsewhere, the BackgroundWorker operates on a different thread than the UI, which could cause conflicts when accessing the same bitmap.

    Try locking the Destination bitmap to ensure exclusive access during the pixel manipulation using the Bitmap.LockBits Method.

    Private Sub BGWProcess_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGWProcess.DoWork
        Dim bmpDataSource As BitmapData = Source.LockBits(New Rectangle(0, 0, Source.Width, Source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
        Dim bmpDataDestination As BitmapData = Destination.LockBits(New Rectangle(0, 0, Destination.Width, Destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
    
        Try
            For Y = 0 To hgt
                For X = 0 To wid
                    If BGWProcess.CancellationPending Then Exit Sub
    
                    Dim srcColor As Color = Color.FromArgb(Marshal.ReadInt32(bmpDataSource.Scan0, (Y * bmpDataSource.Stride) + (X * 4)))
                    
                    Marshal.WriteInt32(bmpDataDestination.Scan0, (Y * bmpDataDestination.Stride) + (X * 4), srcColor.ToArgb())
                Next
    
                BGWProcess.ReportProgress(Y)
            Next
        Finally
            Source.UnlockBits(bmpDataSource)
            Destination.UnlockBits(bmpDataDestination)
        End Try
    End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.