0

这个已经让我发疯了足够长的时间。

启动后台线程以从新线程上的 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
4

1 回答 1

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
于 2013-06-25T22:51:06.843 回答