WPF多线程和旋转图像

时间:2013-06-25 14:31:43

标签: wpf vb.net multithreading

这个让我疯了很长时间。

启动后台线程,从新线程上的Web服务下载数据,然后在状态栏上显示图像并更改文本。

我已尝试使用Dispatcher(具有每个优先级),但在线程子完成之前没有任何反应。我能得到的最接近的是实现相当于至少加载图像和文本的DoEvents,但随后图像停止旋转直到线程完成。

有什么想法吗?

Public Sub Return_DT(ByVal TableName As String)

    CurrentDT = TableName
    If DownloadingDS Is Nothing Then
        DownloadingDS = New Dictionary(Of String, String)
    End If
    If DownloadingDS.ContainsKey(TableName) = False Then
        DownloadingDS.Add(TableName, "Loading")
    Else
        Exit Sub
    End If
    Select Case TableName
        Case "A_Documents"
            strSQL = "SELECT Document_ID, Account_Type, Account_No, Document_Description, Accounts_Only, Open_Editing, Editing_Name, Updated_Name, Updated FROM A_Documents"
        Case Else
            strSQL = "SELECT * FROM " & TableName
    End Select
    ' Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, CType(Sub() LoadMetroImage(), SendOrPostCallback), Nothing)
    'Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, CType(Sub() ChangeLeftStatusText("Downloading " & CurrentDT & " data..."), SendOrPostCallback), Nothing)


            LoadMetroImage()
    ChangeLeftStatusText("Downloading " & CurrentDT & " data...")
   Application.Current.MainWindow.FindName("MainMetroStatusBar")

    Dim vWorker As New BackgroundWorker

    AddHandler vWorker.DoWork, AddressOf BackgroundDownload
    AddHandler vWorker.RunWorkerCompleted, AddressOf DownloadCompleted
    vWorker.RunWorkerAsync()
    DoEvents()

    End Sub

这是我能得到的最接近的

 Public Sub DoEvents()
    Dim frame As New DispatcherFrame()
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf ExitFrame), frame)
    Dispatcher.PushFrame(frame)
End Sub

Public Function ExitFrame(ByVal f As Object) As Object
    CType(f, DispatcherFrame).Continue = False

    Return Nothing
End Function

===编辑===如何调用Return_DT

Public Function DT_Return(ByVal DT As DataTable, ByVal TableName As String) As DataTable
    Try
        If Not DT Is Nothing Then
            If DT_CheckUpdated(TableName) = True Then
                Return DT
            Else
                Return_DT(TableName)
                vService = New Service1Client
                Dim DS As DataSet = vService.ReturnDataSet("SELECT * FROM " & TableName, Current_HOA_ID)
                Dim vDT As DataTable = DS.Tables(0).Copy
                DS.Dispose()
                Return vDT
            End If




        Else
            Return_DT(TableName)
            vService = New Service1Client
            Dim DS As DataSet = vService.ReturnDataSet("SELECT * FROM " & TableName, Current_HOA_ID)
            Dim vDT As DataTable = DS.Tables(0).Copy
            DS.Dispose()
            Return vDT


        End If
    Catch ex As Exception
        EmailError(ex)
        Return Nothing
    End Try
End Function

1 个答案:

答案 0 :(得分:0)

我发现解决方案是在UI线程中使用Dispatcher.Timer来检查每秒是否已经下载了DT ..

 Public Function DT_Return(ByVal DT As DataTable, ByVal TableName As String) As DataTable
    Try
        If Not DT Is Nothing Then
            If DT_CheckUpdated(TableName) = True Then
                Return DT
            Else
                CurrentlyDownloading = True
                Return_DT(TableName)
                Dim vTimer As New DispatcherTimer
                vTimer.Interval = TimeSpan.FromMilliseconds(1000)
                AddHandler vTimer.Tick, Sub(sender As Object, e As EventArgs)
                                            Do While CurrentlyDownloading = True

                                            Loop

                                            vTimer.Stop()
                                        End Sub

                vTimer.Start()
                Return DT
            End If
        Else
            CurrentlyDownloading = True
            Return_DT(TableName)
            Dim vTimer As New DispatcherTimer
            vTimer.Interval = TimeSpan.FromMilliseconds(1000)
            AddHandler vTimer.Tick, Sub(sender As Object, e As EventArgs)
                                        Do While CurrentlyDownloading = True

                                        Loop

                                        vTimer.Stop()
                                    End Sub

            vTimer.Start()
            Return DT

        End If

    Catch ex As Exception
        EmailError(ex)
        Return Nothing
    End Try
End Function
相关问题