Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
Dim tc As New ThreadCollection
#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 cmdStart As System.Windows.Forms.Button
Friend WithEvents cmdResume As System.Windows.Forms.Button
Friend WithEvents cmdPause As System.Windows.Forms.Button
Friend WithEvents cmdStop As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cmdStart = New System.Windows.Forms.Button
Me.cmdPause = New System.Windows.Forms.Button
Me.cmdResume = New System.Windows.Forms.Button
Me.cmdStop = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'cmdStart
'
Me.cmdStart.Location = New System.Drawing.Point(72, 156)
Me.cmdStart.Name = "cmdStart"
Me.cmdStart.Size = New System.Drawing.Size(108, 23)
Me.cmdStart.TabIndex = 0
Me.cmdStart.Text = "Start"
'
'cmdPause
'
Me.cmdPause.Location = New System.Drawing.Point(72, 188)
Me.cmdPause.Name = "cmdPause"
Me.cmdPause.Size = New System.Drawing.Size(108, 23)
Me.cmdPause.TabIndex = 1
Me.cmdPause.Text = "Pause"
'
'cmdResume
'
Me.cmdResume.Location = New System.Drawing.Point(72, 224)
Me.cmdResume.Name = "cmdResume"
Me.cmdResume.Size = New System.Drawing.Size(108, 23)
Me.cmdResume.TabIndex = 2
Me.cmdResume.Text = "Resume"
'
'cmdStop
'
Me.cmdStop.Location = New System.Drawing.Point(72, 256)
Me.cmdStop.Name = "cmdStop"
Me.cmdStop.Size = New System.Drawing.Size(108, 23)
Me.cmdStop.TabIndex = 3
Me.cmdStop.Text = "Stop"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(488, 346)
Me.Controls.Add(Me.cmdStop)
Me.Controls.Add(Me.cmdResume)
Me.Controls.Add(Me.cmdPause)
Me.Controls.Add(Me.cmdStart)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Process1()
' User defined stuff here
End Sub
Private Sub Process2()
' User Defined Stuff here
End Sub
Private Sub Process3()
' User defined stuff here
End Sub
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
Dim p As New Thread(New ThreadStart(AddressOf Process1))
p.Name = "Process1"
tc.Add(p)
p = New Thread(New ThreadStart(AddressOf Process2))
p.Name = "Process2"
tc.Add(p)
p = New Thread(New ThreadStart(AddressOf Process3))
p.Name = "Process3"
tc.Add(p)
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
tc.SuspendAll()
End Sub
Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
tc.ResumeAll()
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
tc.StopAll()
End Sub
End Class
Public Class ThreadCollection
Inherits CollectionBase
Public Sub ResumeAll()
For Each p As Thread In Innerlist
If p.ThreadState = ThreadState.Suspended Then
p.Resume()
End If
Next
End Sub
Public Sub SuspendAll()
For Each p As Thread In Innerlist
If p.ThreadState = ThreadState.Running Then
p.Suspend()
End If
Next
End Sub
Public Sub StopAll()
For Each p As Thread In Innerlist
p.Abort()
' probably need some other cleanup. Perhaps dispose of each
' thread and remove from the collection.
Next
End Sub
Public Function Add(ByVal p As Thread) As Integer
Return Innerlist.Add(p)
End Function
Public Function Remove(ByVal p As Thread)
Innerlist.Remove(p)
End Function
Public Function RemoveRange(ByVal index As Integer, ByVal count As Integer)
innerlist.RemoveRange(index, count)
End Function
Default Public Property Item(ByVal index As Integer) As Thread
Get
If index < 0 Or index > innerlist.Count Then
Throw New IndexOutOfRangeException
Else
Return DirectCast(innerlist.Item(index), Thread)
End If
End Get
Set(ByVal Value As Thread)
innerlist.Item(index) = Value
End Set
End Property
End Class