I have a background worker that calls a form, holding a gif animation. The purpose is to display the animation while process is underway but it should close when the process is done. But it does not close even after completion of the process. Please help.
Thanks
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
BackgroundWorkers are intended to actually do the “work” of the background operation, so the main UI thread can continue rendering things onto the screen. I suspect you want the
GetDataSources()function call to be done within the BackgroundWorker thread.Try switching what’s in your button click function and what’s in the DoWork function of your BackgroundWorker. Specifically, I mean something like this:
And in addition, add some code to the RunWorkerCompleted event to handle what should be done upon completion of your background operation.
You may also want to consider using
frmAnimation.Show()instead offrmAnimation.ShowDialog()depending on if you want the procedure to be modal or modeless. You can read more about that here.