• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

Animated Icon

I have a form that when a button is clicked it triggers another form to popup and reads "Waiting" with an animated spinning arrows gif  
The problem is that when the waiting form is displayed the gif is moving then stops and then moves random.  I think the process, LocalDataCache1SyncAgent() which sync's local sql db to server sql 2005 on the parent form seems to be the problem, but am not sure.  I have tried progress bar and it does the same thing, just does not seem to have movement after the process starts on the parent.  I have even tried starting the process off on the parent form with ..Application.DoEvents() and still does not work..

Any ideas to get a continus smooth running gif while the parent process runs would be greatly appreciated.
0
H-SC
Asked:
H-SC
  • 2
1 Solution
 
klakkasCommented:
The problem is that both forms run in the same thread, so the animation of the first form is blocked by the processing done by the second form.

The solution is to use a backgroundWorker, either in the parent form to call the second one:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Image =anim 'Animation gif
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim f2 As New Form2
        f2.Show()
    End Sub


Or (more efficient in my opinion) use the backgroundWorker in the second form:

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       BackgroundWorker1.RunWorkerAsync()
    End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     'Do the necessary processing
    End Sub

    End Sub
0
 
klakkasCommented:
In general, the best practice is to put all your heavy work in a backgroundWorker, in order to allow the user interface to work without problems.

So, after reading again your description, the best practice ( in my opinion) is the following:

Dim fWait as frmWait
Private Sub btnDoHeavyWork_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoHeavyWork.Click
     fWait = new frmWait
     f.show
     BackgroundWorker1.RunWorkerAsync()
end sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     'Do the necessary processing
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
     fWait.close()
    End Sub
0
 
H-SCAuthor Commented:
klakkas,

Many thanks!  Simply brilliant, not sure why I did not think of the background worker...  It is working smooth!

thanks again
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now