Learn VisualBasic.NET with Me: spinning my wheels, animating an image with the timer

For some reason or other, I didn't get an email response from the client on this gig*, and the week was hectic, so instead, I just worked on making the UI a little nicer. VB has a lot of controls, including a background threader, some standard dialogs, and a serial port. This is giving me a real "Rip Van Winkle" feeling, having been over in Unixville for a while.

(* 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.)

Spinning

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.

  1. Find a gif animation on the web (royalty free).
  2. Use GIMP to split the animation into individual frames.
  3. Number the frames "frame1.bmp", "frame2.bmp", etc.
  4. 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.
  5. Import them into the application resources.
  6. Add a timer control to the form. Set the Enabled property to True.
  7. Add an image control to the form.
  8. 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.

Plugins

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.

AttachmentSize
gearexample.jpg10.01 KB
frame0.bmp24.37 KB
frame1.bmp24.37 KB
frame2.bmp24.37 KB
frame3.bmp24.37 KB