Someone at our local .NET user group asked me about a problem he was having closing a form. He was using a for/next loop to gradually reduce the Opacity property until it hit zero and then he could close his app. He was using Thread.Sleep() inside the loop to control its speed.
Well, the results weren't what he expected. Since Windows Forms apps are single threaded, everything from windows messages to application code all runs on a single thread (unless you place it on another thread: we'll handle that later). So while the for/next loop was reducing the opacity, windows WM_PAINT messages were coming in because the form needed repainting. But there was also that Thread.Sleep in there to control the speed of the fade out. With the thread sleeping, the WM_PAINT messages couldn't be processed. Basically, this wasn't going to give the desired result.
So I whipped up a quick example that shows how to spawn off a seperate thread to fade the form out (as well as performing the fade-out on the main thread to see the negative effects). The second thread does the looping, setting of opacity and sleeping. This way, the main thread is free to handle all of the usual windows messages -- like WM_PAINT, mouse movement, etc...
Option Strict On
Option Explicit On
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
' define our delegate for calling back into the main
' windows thread to update controls
Public Delegate Sub SetOpacity(ByVal d As Double)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' call the fade out on the main thread
FadeOut()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' start a new thread for calling the fade out
Dim t As Thread = New Thread(New ThreadStart(AddressOf FadeOut))
t.IsBackground = True
t.Start()
End Sub
' update our form's opacity
Private Sub SetOp(ByVal d As Double)
Me.Opacity = d
End Sub
' perform the actual fade out
Private Sub FadeOut()
Dim d As Double
For d = 1 To 0 Step -0.01
' if we need to call back to main thread, use our delegate
' otherwise, call the method directly
If Me.InvokeRequired Then
Me.BeginInvoke(New SetOpacity(AddressOf SetOp), New Object() {d})
Else
SetOp(d)
End If
Thread.Sleep(20)
Next
Me.Close()
End Sub
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(104, 24)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Close"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(104, 64)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Thread"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 110)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "Fade Out Sample"
Me.ResumeLayout(False)
End Sub
#End Region
End Class