Hi! First post for me here!
I am usually a pretty self-reliant guy and find my answers by myself. But I haver been struggling to make my BackgroundWorker do what I want of it...
I am working on a small software that pulls info from a database and put it on several datagridview controls. The SQL request pulls quite a lot of data and it takes about 40 seconds to process.
So. I want to put a "Work in progress" animated gif, inside a picturebox, that will be put to visible before the work starts.
Now If have tried just showing the gif beforehand, but predictably, it is jerky, only animating between two SQL calls.
I have tried to put the picturebox into another thread, but I guess I am doing it worng because, while the background worker seems to do it's job, the animation itself seems stuck in the main thread...
Here is the code:
I call a new instance of the Background worker and the delegate:
Private WithEvents wrkDeploy As New System.ComponentModel.BackgroundWorker()
Public Delegate Sub PictureVisibilityDelegate(ByVal visibility As Boolean)
Dim ChangePictureVisibility As PictureVisibilityDelegate
Lines added to the form load, Handlers and delegate link:
AddHandler wrkDeploy.DoWork, AddressOf wrkDeploy_DoWork
AddHandler wrkDeploy.RunWorkerCompleted, AddressOf wrkDeploy_RunWorkerCompleted
ChangePictureVisibility = AddressOf ChangeVisibility
The "Dowork" , "RunworkerComplete" and "ChangeVisibility" subs
Protected Sub wrkDeploy_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles wrkDeploy.DoWork
Me.Invoke(ChangePictureVisibility, True)
End Sub
Protected Sub wrkDeploy_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles wrkDeploy.RunWorkerCompleted
Me.Invoke(ChangePictureVisibility, False)
End Sub
Public Sub ChangeVisibility(ByVal visibility As Boolean)
PicWait.Visible = visibility
End Sub
And the calling before and after my ODBC calls
wrkDeploy = New System.ComponentModel.BackgroundWorker
wrkDeploy.WorkerSupportsCancellation = True
wrkDeploy.RunWorkerAsync()
UnifiedODBC(dgvTickets, 0)
UnifiedODBC(dgvTickets2, 1)
UnifiedODBC(dgvTickets3, 2)
wrkDeploy.CancelAsync()
This is the only way I have gotten it to show... And it still will not show the animation correctly. Furthermore, the pic will animate corretly after the treatment is done but the worker will not hide the picturebox after I cancel it.
Any suggestions?
Thanks