Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27437

Color Cycling Wait Form (Stoner Trap)

$
0
0
There's an odd little something I wanted to add to a program I was working on. It's kind of like a progress bar in marquis style, but I don't like the restrictiveness of the standard progress bar. Therefore, I decided to show a form that can have one or two messages on it, and may or may not show a CANCEL button. Those are options set by the designer. One of the options is that the backcolor of the form and the controls cycles in a steady, hypnotic, pattern. The algorithm used for this cycling is irrelevant, but in case anybody is interested, I'm adding one, small, prime number to the R, adding a different prime number to the G, and subtracting a third prime number from the B. Whenever one byte goes out of range, the sign for the color flips (instead of adding, it subtracts, and so forth). The result is that every possible color will be hit in a pattern that may not repeat precisely in 2^24 iterations.

In any case, the form can be shown with this timer running, and it needs to update. Since this will be happening while other things are happening, the timer is a System.Timers.Timer such that it raises the elapsed event on a threadpool thread. I then do a couple calculations and attempt to update the form using this method:

Code:


    Public Sub SetColors()
        If mCancel = False Then
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf SetColors))
            Else
                Me.BackColor = System.Drawing.Color.FromArgb(curR, curG, curB)
                Me.lMess.BackColor = Me.BackColor
                Me.lWarn.BackColor = Me.BackColor
                Me.lMess2.BackColor = Me.BackColor
                Me.Refresh()
            End If
        End If
    End Sub

The outer If statement, the one with mCancel = False, was just an attempt to get the darn thing to stop. The problem with using a Timers.Timer is that the UI thread can go ahead and dispose of the form while the timer is still ticking, which means that you are trying to access a disposed object in that method. Therefore, I was trying to set a boolean in the Form_Closing event that would stop the whole thing from happening. That didn't work. I get an exception on the Me.Invoke line about accessing a disposed object. That surprised me, because when I got the exception, mCancel was True, which seems like it should have prevented the line from even being reached.

The second issue is that the coloring never happens. I'm not all that familiar with what happens when you alter UI controls from as a result of a background process, since I don't generally do that. There is certainly plenty happening on the UI thread while this timer is running, so I would have to interrupt the current activity of the UI thread to repaint the controls. Perhaps that's unreasonable. Frankly, I don't absolutely need that to happen, but it still seems like the way a progress bar would work. Am I doing it wrong, or thinking about it wrong?

Viewing all articles
Browse latest Browse all 27437

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>