Using WPF polygon in another thread

I had a problem passing an object of type Dictionary (Of Int16, Polygon) as an argument to a background worker so that the background worker can access the Polygons in the dictionary. I get the typical "the calling thread cannot access this object because it has a different message." Here is 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 is of type Dictionary (Of Int16, Polygon). It will contain 1-10 polygons, and I want to transfer this collection to background work, 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 rtb.Render (TempPolygonCollection.Item (CShort (i))). Any help could be helpful. thank.

EDIT: , 2 , , .

: Dictionary (Of Int16, Polygon). Polygon, , BackgroundWorker. Polygon . , ? .

+3
2

Dictionary, , Polygon . WPF, , .

Render ( Dispatcher.Invoke). , , , , , , , , , .

+1

, WPF Polygon , , . , - RenderTargetBitmap.

, Dispatcher, ( Background). , , Dispatcher , .., .

+1

Source: https://habr.com/ru/post/1775873/


All Articles