Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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:

VB
AddHandler wrkDeploy.DoWork, AddressOf wrkDeploy_DoWork
AddHandler wrkDeploy.RunWorkerCompleted, AddressOf wrkDeploy_RunWorkerCompleted
ChangePictureVisibility = AddressOf ChangeVisibility


The "Dowork" , "RunworkerComplete" and "ChangeVisibility" subs
VB
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
VB
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
Posted
Updated 29-Jan-15 5:06am
v5
Comments
Sergey Alexandrovich Kryukov 29-Jan-15 10:21am    
First of all, why animated GIF? What is the content of animation? Wouldn't it better to animate just graphics immediately, if it is simple enough. You don't show ChangePictureVisibility, so it's hard to see what's wrong.
—SA
ThePapaVader 29-Jan-15 10:35am    
It's just a small work in progress gif...

I forgot to include those lines for my formload

AddHandler wrkDeploy.DoWork, AddressOf wrkDeploy_DoWork
AddHandler wrkDeploy.RunWorkerCompleted, AddressOf wrkDeploy_RunWorkerCompleted
ChangePictureVisibility = AddressOf ChangeVisibility


So the ChangeVisisbility sub is the one you are looking for...

I used the code from : http://www.codeproject.com/Questions/535081/Displayingplusanimatedplusgifpluswhilepluscodeplus

How would I go to animated just the graphic? I am still somewhat novice at dotnet...

1 solution

You have the right idea, but are putting the wrong things in the background worker.

The thread your app starts on is also referred to as the UI thread. ALL UI items and iteraction, including your PictureBox and GIF should remain on the UI thread. Your database query work should be in the background worker.
 
Share this answer
 
Comments
ThePapaVader 29-Jan-15 11:37am    
Ok I understand. I tried that and failed miserably as I can't work out how to pass the results of my queries to my datagridviews...

Thanks, I do understant more about vb.net now! :)
Dave Kreskowiak 29-Jan-15 11:48am    
You have to pass the data to a method that sets the DataGridView DataSource property. You'll myForm.Invoke this method from your Background Worker code. This will execute that method on the UI thread.

Remember, all INTERACTIONS with a UI control MUST be done on the UI thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900