Search code examples
wpfvb.netinvokedispatcher

Passing Objects as Arguments to Dispatcher.Invoke in WPF


I have created a BitmapImage Variable in a new thread and need to pass as Argument of Dispatcher.BeginInvoke to UI Thread.

But Error Occurred:

Exception has been thrown by the target of an invocation

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

This is my Code:

Public Delegate Sub NewDeleSub(ByVal InputImage As BitmapImage)
Private MyThread As Thread


Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

    MyThread = New Thread(AddressOf NewThread)
    MyThread.SetApartmentState(ApartmentState.STA)
    MyThread.Start()

End Sub

Private Sub NewThread()

    Dim InImage As New BitmapImage(New Uri("Image Path"))
    Me.Dispatcher.BeginInvoke(New NewDeleSub(AddressOf RotateImage), InImage)

End Sub

Private Sub RotateImage(ByVal Img As BitmapImage)

    Dim OurImg As New Image
    OurImg.Source = Img

End Sub

in these lines error occurred:

Private Sub RotateImage(ByVal Img As BitmapImage)

    Dim OurImg As New Image
    OurImg.Source = Img

End Sub

Solution

  • Finaly, i found answer. It just need to use InImage.Freez to be able to send between Threads.