将对象作为参数传递给WPF中的Dispatcher.Invoke

时间:2014-07-16 21:11:05

标签: wpf vb.net invoke dispatcher

我在新主题中创建了 BitmapImage 变量,需要将 Dispatcher.BeginInvoke 参数传递给 UI主题

但发生了错误:

调用目标抛出了异常

mscorlib.dll中发生未处理的“System.Reflection.TargetInvocationException”类型异常

这是我的代码:

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

在这些行中出现错误:

Private Sub RotateImage(ByVal Img As BitmapImage)

    Dim OurImg As New Image
    OurImg.Source = Img

End Sub

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案。它只需要使用InImage.Freez就可以在线程之间发送。