0

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
GiGatR00n
  • 116
  • 8
  • The Error is Exception has been thrown by the target of an invocation – GiGatR00n Jul 16 '14 at 21:17
  • **String** & **Integers** can Pass as Argument to Dispatcher.Invoke but Object Variable Cant. How to send Objects? – GiGatR00n Jul 16 '14 at 21:19
  • Why do you think object can't be passed? don't derive that from your problem. I guess that you have more code in the `RotateImage` method? because with just those 2 lines of code, I don't think there is any exception here, the `OurImg` is just a new control and is not even set with any parent control. – King King Jul 16 '14 at 21:20
  • The Problem is that after sending **BitmapImage** Object to sub **RotateImage**, Error Occurred. – GiGatR00n Jul 16 '14 at 21:25
  • What is the exact line at which the exception is thrown? what if trying commenting out that line (or even all the code inside `RotateImage`)? – King King Jul 16 '14 at 21:30
  • exact error line: **OurImg.Source = Img** – GiGatR00n Jul 16 '14 at 21:43
  • I confused because If I send **String** or **Integers** instead of BitmapImage Object, It work – GiGatR00n Jul 16 '14 at 21:47
  • What is the actual error message? Wrap the code that throws the exception in an exception handler and LOOK at the exception. – jmcilhinney Jul 17 '14 at 01:26

1 Answers1

0

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

GiGatR00n
  • 116
  • 8