Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a WPF Polygon in another thread

I'm having an issue when passing an object of type Dictionary(Of Int16, Polygon) as an argument to a backgroundworker so the background worker is able to access the Polygons in the Dictionary. I'm getting the typical "The calling thread cannot access this object because a different thread owns it" message. Heres what i have:

                Dim worker As New BackgroundWorker
                AddHandler worker.DoWork, AddressOf MeasurePolygons
                AddHandler worker.RunWorkerCompleted, AddressOf WorkerCompleted

                worker.RunWorkerAsync(PolygonCollection)

PolygonCollection is a private variable declared at the top of the class and it is of type Dictionary(Of Int16, Polygon). It will contain 1-10 polygons in it and i want to pass this collection to the backgroundworker because i do calculations on each polygon in the dictionary.

    Dim TempPolygonCollection As Dictionary(Of Int16, Polygon)
    TempPolygonCollection = CType(e.Argument, Dictionary(Of Int16, Polygon))
    For i = 0 To TempPolygonCollection.Count - 1
            If TempPolygonCollection.ContainsKey(CShort(i)) Then
                Dim rtb As New RenderTargetBitmap(CInt(800), CInt(600), 96D, 96D, PixelFormats.Default)
                rtb.Render(TempPolygonCollection.Item(CShort(i)))
                Dim encoder As New BmpBitmapEncoder
                encoder.Frames.Add(BitmapFrame.Create(rtb))
            End If
    Next i

the error occurs on the rtb.Render(TempPolygonCollection.Item(CShort(i))). Any help would be really appreciated. thanks.

EDIT: So the 2 friendly posters helped me narrow down my issue but I'm still stuck on how to figure it out.

The Current Problem: I have an object of type Dictionary(Of Int16, Polygon). I need to be able to access each individual Polygon,which are created on the UI thread, inside this Dictionary with the BackgroundWorker. Then on the backgroundworker i will be creating a bitmap file based on the Polygon in the Dictionary. So how can I get access to the Polygons on the backgroundworker? Thanks for any help.

like image 862
devman Avatar asked Feb 25 '26 00:02

devman


2 Answers

The problem is not the Dictionary, the problem is the Polygon stored in the Dictionary. These are WPF UI elements created in the UI thread, so only the UI thread can use them.

You could execute the Render operation into the UI thread (using Dispatcher.Invoke). This would mean that the rendering blocks your UI thread, but at least in between the render operations, the UI thread would be able to handle other things, i.e., the UI should stay responsive if the time required for a single render operation is not too long.

like image 64
Heinzi Avatar answered Feb 26 '26 13:02

Heinzi


The problem is that in WPF, the Polygon objects can't be used on a thread other than the one that created them. This means that you can't access any of their properties, or attempt to render them into a RenderTargetBitmap.

If you are trying to not block the UI while rendering all these images, I would suggest using the Dispatcher to break up the work (schedule them at the Background priority). That way, while an individual save is happening on the UI thread, the Dispatcher can interleave mouse input, rendering, etc. so the UI won't get blocked.

like image 38
Abe Heidebrecht Avatar answered Feb 26 '26 14:02

Abe Heidebrecht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!