I needed something very similar. What I did is as follows.
1. Create an Activity form with a progress bar on it.
2. Create an activity class
3. In the class I have an Activity start property, when this is called
it creates a seperate background thread that calls a function. This
function creates an instance of another class (which is running in the
new thread, it then create an instance of the activity form and handles
updating of the progress bar automatically. This is the way I did it, I
cannot say if this is the best or correct way, but this does work for
me.
Code Below
'************************************************* **********
' Author: Adelio Stevanato
' Activity form, that shows that something is happening.
' All this code is to allow me to show a stateless actvity
' form. in this case a progress bat that cycles 1-100
' then restarts at 1
' the form is NOT called directly but from a class
' called clsActivity
' This handles all the thread stuff
'************************************************* **********
'Friend - Activity form, not accessable elsewhere
Friend Class frmActive
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
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 lblInfo As System.Windows.Forms.Label
Friend WithEvents BackgroundWorker1 As
System.ComponentModel.BackgroundWorker
Friend WithEvents PBar As System.Windows.Forms.ProgressBar
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.lblInfo = New System.Windows.Forms.Label
Me.PBar = New System.Windows.Forms.ProgressBar
Me.BackgroundWorker1 = New
System.ComponentModel.BackgroundWorker
Me.SuspendLayout()
'
'lblInfo
'
Me.lblInfo.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.lblInfo.BackColor = System.Drawing.SystemColors.Window
Me.lblInfo.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D
Me.lblInfo.Location = New System.Drawing.Point(0, 0)
Me.lblInfo.Name = "lblInfo"
Me.lblInfo.Size = New System.Drawing.Size(320, 39)
Me.lblInfo.TabIndex = 1
Me.lblInfo.Text = "Processing...."
Me.lblInfo.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'PBar
'
Me.PBar.Dock = System.Windows.Forms.DockStyle.Bottom
Me.PBar.Location = New System.Drawing.Point(0, 36)
Me.PBar.Name = "PBar"
Me.PBar.Size = New System.Drawing.Size(320, 20)
Me.PBar.Step = 1
Me.PBar.TabIndex = 2
'
'BackgroundWorker1
'
'
'frmActive
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 56)
Me.ControlBox = False
Me.Controls.Add(Me.PBar)
Me.Controls.Add(Me.lblInfo)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedToolWind ow
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "frmActive"
Me.ShowInTaskbar = False
Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterParen t
Me.TopMost = True
Me.Controls.SetChildIndex(Me.lblInfo, 0)
Me.Controls.SetChildIndex(Me.PBar, 0)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Dim mstrInfo As String
Dim m_Val As Integer = 0
Dim m_owner As Form
Dim m_FormLocation As clsActivity.PositionLocation =
clsActivity.PositionLocation.Centre
Public Sub New(ByVal message As String)
Me.New()
mstrInfo = message
Info = mstrInfo
End Sub
Public Sub New(ByVal message As String, ByVal Owner As Form)
Me.New()
mstrInfo = message
Info = mstrInfo
m_owner = Owner
End Sub
Public Property FormLocation() As clsActivity.PositionLocation
Get
Return m_FormLocation
End Get
Set(ByVal Value As clsActivity.PositionLocation)
If m_FormLocation <Value Then
m_FormLocation = Value
If Me.Visible = True Then
Call PositionForm()
End If
End If
End Set
End Property
Public Property Info() As String
Get
Return mstrInfo
End Get
Set(ByVal Value As String)
mstrInfo = Value
lblInfo.Text = mstrInfo
Me.Refresh()
End Set
End Property
Public Sub StartActivity()
lblInfo.Text = mstrInfo
If Me.Visible = False Then
Me.Show()
End If
Call PositionForm()
Me.BringToFront()
m_Val = 0
End Sub
Public Shadows ReadOnly Property Owner() As Form
Get
Return m_owner
End Get
End Property
Private Sub PositionForm()
If m_FormLocation = clsActivity.PositionLocation.Centre Then
If m_owner Is Nothing Then
CentreForm(Me)
Else
If m_owner.MdiParent Is Nothing Then
Me.Location = New Point(CInt((m_owner.Left +
(m_owner.Width / 2))) - CInt(Me.Width / 2), m_owner.Top +
CInt(m_owner.Height / 2) - CInt(Me.Height / 2))
Else
If Not m_owner.MdiParent Is Nothing Then
Me.Location = New Point((m_owner.Left +
m_owner.MdiParent.Left + CInt(m_owner.Width / 2)) - CInt(Me.Width / 2),
m_owner.Top + m_owner.MdiParent.Top + CInt(m_owner.Height / 2) -
CInt(Me.Height / 2))
Else
CentreForm(Me)
End If
End If
End If
Else
If m_FormLocation = clsActivity.PositionLocation.BottomRight
Then
If m_owner Is Nothing OrElse m_owner.MdiParent Is
Nothing Then
Me.Left = Screen.PrimaryScreen.Bounds.Width -
Me.Width
Me.Top = Screen.PrimaryScreen.Bounds.Height -
Me.Height
Else
Me.Left = m_owner.MdiParent.Right - (Me.Width + 8)
Me.Top = m_owner.MdiParent.Bottom - (Me.Height + 31)
End If
Else
If m_owner Is Nothing OrElse m_owner.MdiParent Is
Nothing Then
Me.Left = Screen.PrimaryScreen.Bounds.Left + 8
Me.Top = Screen.PrimaryScreen.Bounds.Height -
Me.Height
Else
Me.Left = m_owner.MdiParent.Left + 6
Me.Top = m_owner.MdiParent.Bottom - (Me.Height + 31)
End If
End If
End If
Me.Refresh()
End Sub
Private Sub EndActivity(ByVal CloseForm As Boolean)
m_Val = 0
If CloseForm Then
Me.Close()
End If
End Sub
Public Sub EndActivity()
Me.Close()
End Sub
Public Sub DoStep()
Call DoStep(-1)
End Sub
Public Sub DoStep(ByVal Value As Integer)
If Value 0 AndAlso Value <= 100 Then
m_Val = Value
Else
m_Val += 1
If m_Val 100 Then
m_Val = 1
End If
End If
PBar.Value = m_Val
End Sub
End Class
' PUBLIC Calling Class
Public Class clsActivity
Implements IDisposable
Public Enum PositionLocation
Centre = 0
BottomRight = 1
BottomLeft = 2
End Enum
Private Enum ActivityMode
Unknown = 0
StartActivity = 1
EndActivity = 2
PauseActivity = 3
ResumeActivity = 4
End Enum
Public Enum ActivityStatus
Uninitalised = 0
Running = 1
Ended = 2
Pauses = 3
End Enum
Private HasStarted As Boolean = False
Private m_Th As System.Threading.Thread
Private m_mode As ActivityMode
Private m_CurrentStatus As ActivityStatus =
ActivityStatus.Uninitalised
Private m_modeChanged As Boolean = False
Private m_MaxDisplayTimeSecs As Long = (10 * 60) 'Max time to
display Activity display (3 mins)
Private m_StartTime As Date
Private m_ThreadInterval As Integer = 10
Private m_Info As String = "Processing..."
Private m_Owner As Form
Dim m_FormLocation As PositionLocation = PositionLocation.Centre
Public Sub New()
End Sub
Public Sub New(ByVal Info As String)
m_Info = Info
End Sub
Public Sub New(ByVal Info As String, ByVal Owner As
System.Windows.Forms.Form)
m_Info = Info
m_Owner = Owner
End Sub
'Specifies the interval in Milliseconds between updates
Public Property UpdateInterval() As Integer
Get
Return m_ThreadInterval
End Get
Set(ByVal Value As Integer)
If Value 0 AndAlso Value < 1000 Then
m_ThreadInterval = Value
End If
End Set
End Property
Public Property FormLocation() As PositionLocation
Get
Return m_FormLocation
End Get
Set(ByVal Value As PositionLocation)
If m_FormLocation <Value Then
m_FormLocation = Value
End If
End Set
End Property
'Specifies the Max amount of time to have the activity runnimg,
Default to 3 minutes
Public Property MaxDisplayTimeSecs() As Long
Get
Return m_MaxDisplayTimeSecs
End Get
Set(ByVal Value As Long)
m_MaxDisplayTimeSecs = Value
End Set
End Property
Public ReadOnly Property CurrentStatus() As ActivityStatus
Get
Return m_CurrentStatus
End Get
End Property
Public ReadOnly Property Owner() As Form
Get
Return m_Owner
End Get
End Property
Public Property Info() As String
Get
Return m_Info
End Get
Set(ByVal Value As String)
m_Info = Value
End Set
End Property
Public Sub Startup()
If Not HasStarted Then
HasStarted = True
m_mode = ActivityMode.StartActivity
m_modeChanged = True
m_Th = New System.Threading.Thread(AddressOf DoActivity)
m_Th.IsBackground = True
m_Th.Start()
End If
End Sub
<System.ComponentModel.Description("End the activity display")_
Public Sub EndActivity()
Mode = ActivityMode.EndActivity
End Sub
<System.ComponentModel.Description("Pause the activity display")_
Public Sub PauseActivity()
Mode = ActivityMode.PauseActivity
End Sub
<System.ComponentModel.Description("Resume the activity display")_
Public Sub ResumeActivity()
Mode = ActivityMode.ResumeActivity
End Sub
Private Property Mode() As ActivityMode
Get
Return m_mode
End Get
Set(ByVal Value As ActivityMode)
m_mode = Value
m_modeChanged = True
If Value = ActivityMode.EndActivity Then
System.Threading.Thread.Sleep(20)
If Not m_Th Is Nothing Then
System.Threading.Thread.Sleep(75)
Dim tm As Date = Now
While Not m_Th Is Nothing
System.Threading.Thread.Sleep(25)
If m_Th.ThreadState And
(Threading.ThreadState.Stopped) = 0 OrElse m_Th.ThreadState And
(Threading.ThreadState.Aborted) = 0 Then
m_Th = Nothing
Else
If DateDiff(DateInterval.Second, tm, Now) >
0 Then
m_Th.Abort()
m_Th = Nothing
End If
End If
End While
End If
End If
End Set
End Property
'Class run in separate thread
Private Sub DoActivity()
#If DEBUG Then
Try
#End If
Dim Act As Activity
While True
'This code checks if the form has been disposed, if it
has DO NOT DO ANYTHING
If Not Act Is Nothing AndAlso
Act.Activityform.IsDisposed = True Then
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
'IF the thread has been aborted then Exit from the loop
If System.Threading.Thread.CurrentThread.ThreadState And
(Threading.ThreadState.Aborted) = 0 Then
pos = "1a"
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
'
'If activity form active for too long then Stop Activity
'
If m_CurrentStatus = ActivityStatus.Running AndAlso
DateDiff(DateInterval.Second, m_StartTime, Now) m_MaxDisplayTimeSecs
Then
m_mode = ActivityMode.EndActivity
m_modeChanged = True
If Not Act Is Nothing Then
Act.EndActivity()
End If
m_CurrentStatus = ActivityStatus.Ended
#If DEBUG Then
Throw New Exception("clsActivity::DoActivity :-
Timeout of activity status, max= " & m_MaxDisplayTimeSecs & " Seconds")
#End If
End If
'IF the thread has been aborted then Exit from the loop
If System.Threading.Thread.CurrentThread.ThreadState And
(Threading.ThreadState.Aborted) = 0 Then
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
' This codee only called if the mode as changed
If m_modeChanged = True Then
'I want to start
If m_CurrentStatus <ActivityStatus.Running AndAlso
Mode = ActivityMode.StartActivity Then
If Act Is Nothing Then
Act = New Activity(Info, Owner)
Act.Info = Info
Act.FormLocation = m_FormLocation
m_CurrentStatus = ActivityStatus.Running
Act.StartActivity()
m_StartTime = Now
End If
'I want to end
ElseIf m_mode = ActivityMode.EndActivity Then
If Not Act Is Nothing Then
Act.EndActivity()
End If
m_CurrentStatus = ActivityStatus.Ended
Exit While
'I want to pause
ElseIf m_CurrentStatus = ActivityStatus.Running
AndAlso m_mode = ActivityMode.PauseActivity Then
If Not Act Is Nothing Then
Act.PauseActivity()
m_CurrentStatus = ActivityStatus.Pauses
End If
'Iwant to resume a pause
ElseIf m_CurrentStatus = ActivityStatus.Pauses
AndAlso m_mode = ActivityMode.ResumeActivity Then
If Not Act Is Nothing Then
Act.ResumeActivity()
m_CurrentStatus = ActivityStatus.Running
End If
End If
m_modeChanged = False
End If
'IF the thread has been aborted then Exit from the loop
If System.Threading.Thread.CurrentThread.ThreadState And
(Threading.ThreadState.Aborted) = 0 Then
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
'If running then refresh form and do another step
If m_CurrentStatus <ActivityStatus.Ended AndAlso
m_CurrentStatus <ActivityStatus.Uninitalised Then
If Not Act Is Nothing Then
If Act.Info <m_Info Then
Act.Info = m_Info
End If
Act.FormLocation = m_FormLocation
Act.DoStep()
End If
End If
'IF the thread has been aborted then Exit from the loop
If Not
(System.Threading.Thread.CurrentThread.ThreadState And
(Threading.ThreadState.Aborted) = 0) Then
'Sleep for a little bit, this controls how quickly
the progress bar on the activity form steps
System.Threading.Thread.Sleep(m_ThreadInterval)
Else
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
'This code checks if the form has been disposed, if it
has DO NOT DO ANYTHING
If Not Act Is Nothing AndAlso
Act.Activityform.IsDisposed = True Then
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
Application.DoEvents()
'IF the thread has been aborted then Exit from the loop
If System.Threading.Thread.CurrentThread.ThreadState And
(Threading.ThreadState.Aborted) = 0 Then
m_CurrentStatus = ActivityStatus.Ended
Exit While
End If
End While
#If DEBUG Then
Catch ex As Exception
MsgBox(ex.Message)
End Try
#End If
End Sub
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not m_Th Is Nothing Then
m_mode = ActivityMode.EndActivity
End If
End If
End Sub
Protected Overrides Sub Finalize()
m_mode = ActivityMode.EndActivity
m_CurrentStatus = ActivityStatus.Ended
MyBase.Finalize()
End Sub
End Class
' This is a Friend class as well.
Friend Class Activity
Private m_ActivityForm As frmActive
Private m_Pause As Boolean = False
Private m_started As Boolean = False
Private m_Owner As Form
Private m_formLocation As clsActivity.PositionLocation
<System.ComponentModel.Description("Default Constructor")_
Public Sub New()
m_ActivityForm = New frmActive
End Sub
<System.ComponentModel.Description("Default Constructor with an
initial activity message")_
Public Sub New(ByVal Info As String)
m_ActivityForm = New frmActive(Info)
End Sub
<System.ComponentModel.Description("Default Constructor with an
initial activity message")_
Public Sub New(ByVal Info As String, ByVal Owner As Form)
m_Owner = Owner
m_ActivityForm = New frmActive(Info, Owner)
End Sub
<System.ComponentModel.Description("The Activity Form")_
Public ReadOnly Property Activityform() As frmActive
Get
Return m_ActivityForm
End Get
End Property
Public Property FormLocation() As clsActivity.PositionLocation
Get
Return m_FormLocation
End Get
Set(ByVal Value As clsActivity.PositionLocation)
If m_FormLocation <Value Then
m_formLocation = Value
If Not m_ActivityForm Is Nothing Then
m_ActivityForm.FormLocation = m_formLocation
End If
End If
End Set
End Property
<System.ComponentModel.Description("Change the current activity
mesage being displayed")_
Public Property Info() As String
Get
Return m_ActivityForm.Info
End Get
Set(ByVal Value As String)
m_ActivityForm.Info = Value
End Set
End Property
Public Sub DoStep()
If m_started = True AndAlso m_Pause = False Then
m_ActivityForm.DoStep()
End If
End Sub
<System.ComponentModel.Description("The Activity Form")_
Public Sub StartActivity(ByVal Info As String)
m_started = True
m_Pause = False
Call m_ActivityForm.StartActivity()
End Sub
<System.ComponentModel.Description("Start the activity display")_
Public Sub StartActivity()
m_started = True
m_Pause = False
Call m_ActivityForm.StartActivity()
End Sub
<System.ComponentModel.Description("End the activity display")_
Public Sub EndActivity()
m_started = False
m_Pause = False
Call m_ActivityForm.EndActivity()
m_ActivityForm = Nothing
m_ActivityForm = New frmActive
End Sub
<System.ComponentModel.Description("Pause the activity display")_
Public Sub PauseActivity()
m_Pause = True
End Sub
<System.ComponentModel.Description("Resume the activity display")_
Public Sub ResumeActivity()
m_Pause = False
End Sub
Protected Overrides Sub Finalize()
If Not m_ActivityForm Is Nothing Then
m_ActivityForm.EndActivity()
m_ActivityForm = Nothing
End If
MyBase.Finalize()
End Sub
End Class
*** Sent via Developersdex
http://www.developersdex.com ***