2013-02-03 99 views
0

我有一個後臺工作人員調用一個表單,並持有gif動畫。目的是在進程正在進行時顯示動畫,但當進程完成時它應該關閉。但即使完成該過程,它也不會關閉。請幫忙。
感謝處理後臺工作不起作用

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
    frmAnimation.ShowDialog() 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    BackgroundWorker1.RunWorkerAsync() 
    Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance 
    datatable1 = sqldatasourceenumerator1.GetDataSources() 
    DataGridView1.DataSource = datatable1 

    'I have tried CancelAsync, but did not work 

    BackgroundWorker1.CancelAsync() 
    frmAnimation.Dispose() 
End Sub 
+0

是frmAnimation的類型或實例 – user1937198

回答

1

BackgroundWorkers旨在真正做到後臺操作的「工作」,所以在主UI線程可以繼續渲染的東西在屏幕上。我懷疑你想要在BackgroundWorker線程內完成GetDataSources()函數調用。

嘗試切換按鈕點擊功能以及BackgroundWorker的DoWork功能。具體來說,我的意思是這樣:

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
      Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance 
      datatable1 = sqldatasourceenumerator1.GetDataSources() 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
      BackgroundWorker1.RunWorkerAsync() 
      frmAnimation.ShowDialog() 
    End Sub 

,此外,一些代碼添加到RunWorkerCompleted事件處理什麼都要你的後臺操作完成後進行。

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
      DataGridView1.DataSource = datatable1 
      frmAnimation.Close() 
    End Sub 

您可能還需要考慮不同,如果你希望該過程是模式或無模式上使用的frmAnimation.Show()代替frmAnimation.ShowDialog()。您可以閱讀關於here的更多信息。