Animation and multi-threading - NOT!

G

Guest

I've tried changing my animator user control to run of off a separate thread, but it acts like it is still sharing the main thread and freezes whenever the main code is blocked

Maybe this is because I am doing all of this inside of one user control? All of the MSDN samples define the callback proc in a separate module. Is this a requirement? Or maybe I am running into trouble because I am referencing class module-level variables inside of my callback proc

Here's what I have (stripped down for a shorter posting)

Public Class AnimatedPictureBox
Inherits System.Windows.UserContro

Private AnimateDelegate as System.Threading.TimerCallBac
Private AnimateTimer as System.Threading.Time
Private CurrentCellNumber as Integer
private StaticCellNumber as Integer
...

Later, in the same clas

Public Sub StartAnimation(
Me.StopAnimation ' clear any previous calls
AnimateDelegate = New System.Threading.TimerCallBack ( AddressOf Me.Animate
AnimateTimer = New System.Threading.Timer ( AnimateDelegate, Nothing, 1, 55
End Su

Public Sub Stop Animation(
AnimateDelegate = Nothin
AnimateTimer = Nothin
DisplayImage (StaticCellNumber
End Su

' the callback pro
Private Sub Animate()
CurrentCellNumber +=
If CurrentCellNumber = StaticCellNumber Then CurrentCellNumber +=
If CurrentCell >= Me.ImageList.Images.Count Then CurrentCellNumber =
DisplayImage (CurrentCellNumber
End Sub

Private Sub DisplayImage(ByVal CellNumber as Integer
Me.PictureBox.Image = Me.ImageList.Images(CellNumber
Me.PictureBox.Invalidate ' force a paint to show the new imag
End Sub

End Class
 
R

Rick Powell

Try reversing the threads.
I believe that the form is controlled by the main thread, thus your
animation must also be in the main thread to work.

Do your other code in the spawned thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top