2013-06-25 37 views
0

這個驅動我堅持了足夠長的時間。WPF多線程和旋轉圖像

啓動一個後臺線程,從一個新線程上的webservice下載數據,然後在狀態欄上顯示圖像並更改文本。

我已經嘗試過調度程序(每個優先級),但沒有任何反應,直到螺紋子完成。我能得到的最接近的是實現相當於至少加載圖像和文本的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 
+0

你在'BackgroundDownload'中與UI進行交互嗎? – Pragmateek

+0

嗨Pragmateek - 我已經嘗試從後臺線程調度(沒有做任何事情),但當然,沒有直接與用戶界面的互動 – gchq

+0

您使用哪個組件下載,一個WebClient? – Pragmateek

回答

0

我找到了解決方案是使用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