I am working on a generic way to launch multiple similar processes (threads)
at once, but limit the number of threads running at any one time to a number
I set. As I understand it the following line makes a Queue "thread safe", so
I do not need to explicitly lock and unlock it when multiple threads are
working with it.
'Thread safe queue
Private IndexQueue As Queue = Queue.Synchronized(New Queue)
The following code seems to demonstrate this point quite well. However,
there is one oddity I do not understand. I have to introduce a delay
(Thread.CurrentThread.Sleep(5)) after launching each thread or else I get an
error that the queue is empty when I try to dequeue the next item. This 5 ms
delay is no big deal in this example, however I have found that if I am
doing actual work I have had to increase this value to 300, 500 or even 1000
to avoid the error.
Can anyone tell me what is going on?
Dave Coate
Option Strict On
Imports System.Threading
Public Class Form1
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()
'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 lblThreadCount As System.Windows.Forms.Label
Friend WithEvents lblTotal As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lblFinished As System.Windows.Forms.Label
Friend WithEvents btnGo As System.Windows.Forms.Button
Friend WithEvents dgMailboxes As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.lblThreadCount = New System.Windows.Forms.Label
Me.lblTotal = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.lblFinished = New System.Windows.Forms.Label
Me.btnGo = New System.Windows.Forms.Button
Me.dgMailboxes = New System.Windows.Forms.DataGrid
CType(Me.dgMailboxes,
System.ComponentModel.ISupportInitialize).BeginIni t()
Me.SuspendLayout()
'
'lblThreadCount
'
Me.lblThreadCount.Anchor =
CType((System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.lblThreadCount.Location = New System.Drawing.Point(8, 347)
Me.lblThreadCount.Name = "lblThreadCount"
Me.lblThreadCount.TabIndex = 11
Me.lblThreadCount.Text = "0"
'
'lblTotal
'
Me.lblTotal.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.lblTotal.Location = New System.Drawing.Point(280, 347)
Me.lblTotal.Name = "lblTotal"
Me.lblTotal.Size = New System.Drawing.Size(48, 23)
Me.lblTotal.TabIndex = 10
Me.lblTotal.Text = "0"
'
'Label2
'
Me.Label2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.Label2.Location = New System.Drawing.Point(240, 347)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(24, 23)
Me.Label2.TabIndex = 9
Me.Label2.Text = "Of"
'
'lblFinished
'
Me.lblFinished.Anchor =
CType((System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lblFinished.Location = New System.Drawing.Point(192, 347)
Me.lblFinished.Name = "lblFinished"
Me.lblFinished.Size = New System.Drawing.Size(32, 23)
Me.lblFinished.TabIndex = 8
Me.lblFinished.Text = "0"
'
'btnGo
'
Me.btnGo.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnGo.Location = New System.Drawing.Point(336, 347)
Me.btnGo.Name = "btnGo"
Me.btnGo.TabIndex = 7
Me.btnGo.Text = "Go"
'
'dgMailboxes
'
Me.dgMailboxes.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.dgMailboxes.DataMember = ""
Me.dgMailboxes.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.dgMailboxes.Location = New System.Drawing.Point(0, 3)
Me.dgMailboxes.Name = "dgMailboxes"
Me.dgMailboxes.Size = New System.Drawing.Size(416, 328)
Me.dgMailboxes.TabIndex = 6
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 373)
Me.Controls.Add(Me.lblThreadCount)
Me.Controls.Add(Me.lblTotal)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.lblFinished)
Me.Controls.Add(Me.btnGo)
Me.Controls.Add(Me.dgMailboxes)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.dgMailboxes,
System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)
End Sub
#End Region
Private m_dt As DataTable
Private LastIndex As Integer
Private IndexQueue As Queue = Queue.Synchronized(New Queue)
Private m_CurrentRowIndex As Integer
Private ThreadCount As Integer
Private FinishedCount As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Create and show a datatable of items to be processed and track
progress
m_dt = New DataTable
m_dt.Columns.Add("Index")
m_dt.Columns.Add("Delay")
m_dt.Columns.Add("ThreadState")
m_dt.Columns.Add("Result")
For ix As Integer = 0 To 10
Dim NewRow As DataRow = m_dt.NewRow
With NewRow
.Item("Index") = ix
.Item("Delay") = CInt(Rnd(ix) * 10000)
End With
m_dt.Rows.Add(NewRow)
Next
dgMailboxes.DataSource = m_dt
End Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
LastIndex = m_dt.Rows.Count - 1
lblTotal.Text = CStr(LastIndex + 1)
'Fill a queue of items (index to table) to process
For m_CurrentRowIndex = 0 To LastIndex
IndexQueue.Enqueue(m_CurrentRowIndex)
Next
'Process items in the queue
While IndexQueue.Count > 0
lblThreadCount.Text = CStr(ThreadCount)
lblThreadCount.Refresh()
'Launch a new thread if there are less than 20 threads running
If ThreadCount < 20 Then
Dim t As New Thread(AddressOf RunDummyTask)
t.IsBackground = False
t.Start()
End If
'Refresh Gui objects
dgMailboxes.Refresh()
lblTotal.Refresh()
lblThreadCount.Refresh()
'needed while threads are starting
lblFinished.Text = CStr(FinishedCount)
lblFinished.Refresh()
'Program crashes without this line. Why?
'Error: Queue Empty
Thread.CurrentThread.Sleep(5)
End While
End Sub
Private Sub RunDummyTask()
'Increment thread count
ThreadCount += 1
'Dequeue the next index
Dim Index As Integer = CInt(IndexQueue.Dequeue)
'Get data about the process to run
Dim Delay As Integer = CInt(m_dt.Rows(Index)("Delay"))
'Show progress of the thread in the GUI
m_dt.Rows(Index)("ThreadState") = "Started"
'Run Process
Thread.CurrentThread.Sleep(Delay)
'Show progress of the thread in the GUI
m_dt.Rows(Index)("Result") = "Completed"
'Decrement the thread count
ThreadCount -= 1
'track progress in GUI
FinishedCount += 1
'needed after all threads have started
lblFinished.Text = CStr(FinishedCount)
'If this is the last thread...
If FinishedCount = LastIndex + 1 Then
Done()
End If
End Sub
Private Sub Done()
MsgBox("Done")
End Sub
End Class