Learn VisualBasic.NET with Me: spinning my wheels, animating an image with the timer
(* It was lost in my huge email pile. My bad. No matters… the program got a nice facelift.)
I downloaded a bunch of code examples, and started looking through them. The second volume had some interesting programs. One thing hasn’t changed about VB – you have to type a lot of code to get a little done. At least that’s still true if you’re writing algorithmic code… and clearly, C#’s job is to write that algorithmic code.
Speaking of too much code – I may have written a little too much to do my batch job thread. The example puts the threading code into the form’s class, while my code does it in a separate class. My way required sending a lot of events from the class to the GUI. It’s probably more “by the book” my way, but the example’s style is a lot simpler-looking. The only downside is that the logic is embedded in the GUI’s code.
(Also, someone on devshed told me that it’s better to do things the MS way. I tend to agree, and if I recode this into C#, I’ll refactor that bit out and do the the MS way. That will require rewriting the doc processor to do one doc at a time.)
I wonder if my title was subconcious, because, in the past hour, I’ve added animated gears to the appliction. Now, when the scheduler thread is running, it’s indicated by a little cartoon of gears spinning.
It’s kind of ugly, but, it gets the point across. Here’s how to do it.
- Find a gif animation on the web (royalty free).
- Use GIMP to split the animation into individual frames.
- Number the frames “frame1.bmp”, “frame2.bmp”, etc.
- Take one of the images, save it as “frame0.bmp”. This is the image displayed when the thread is not running, so you might want to darken it, or desaturate it, for effect.
- Import them into the application resources.
- Add a timer control to the form. Set the Enabled property to True.
- Add an image control to the form.
- Use the code below as a template for your own. The variable t is the running thread.
Private frame As Int16 = 1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If t IsNot Nothing Then If t.IsAlive Then frame = frame + 1 If frame = 4 Then frame = 1 Me.PictureBox1.Image = My.Resources.ResourceManager.GetObject("frame" & frame) Exit Sub End If End If Me.PictureBox1.Image = My.Resources.frame0 End Sub
Attached are the four frames. I got them from a free animation I found via Google images.
Writing Plugin-Based Applications explains how to use .DLLs as .NET plugins. It involves reading a directory of DLLs, loading each, and checking if each one implements the plugin interface. If it’s valid, the instance can be called by the host.
Plugins are probably overkill for this little app, but, if I have to alter it more than a couple more times, the plugins will make sense.