从不同的线程VB.net更新WPF UI

时间:2016-02-22 20:28:20

标签: wpf vb.net multithreading user-interface

我正在尝试更新WPF图像控件。使用VB.net。

我尝试了很多使用Delegates和不同Invoke方法的方法。但是我仍然得到相同的错误:

  

WindowsBase.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理。附加信息:调用线程无法访问此对象,因为其他线程拥有它。

bmp 是我想用来更新名为 iMain 的ImageControl的值。

这是我的当前代码:

 Dim uri As New Uri(strPhotoshootPath & "\" & strFilename)
        Dim bmp As New BitmapImage(uri)
        'Opens the Image so that it can be Displayed on the Control.

        iMain.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New SetImageControlCallback(
            AddressOf SetImageControl), bmp)
        'Loads the Image into the Control. By Dispatching the Call to the UI Thread.

        uri = Nothing
        'Sets the Variables to Null
    Catch ex As Exception
        Debug.WriteLine("Message: " & ex.Message & " Source: " & ex.Source)
        MessageBox.Show("An Error Ocurred. " & ex.Message)
    End Try
End Sub

Delegate Sub SetImageControlCallback(ByVal bmp As BitmapImage)
Friend Sub SetImageControl(ByVal bmp As BitmapImage)
    iMain.Source = bmp
End Sub

有关如何完成此任务的任何想法? 感谢。

0 个答案:

没有答案