2013-02-03 96 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的更多信息。