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.Synchroni zed(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.Current Thread.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.Threadin g
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.lblThreadCou nt = 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.dgMail boxes,
System.Componen tModel.ISupport Initialize).Beg inInit()
Me.SuspendLayou t()
'
'lblThreadCount
'
Me.lblThreadCou nt.Anchor =
CType((System.W indows.Forms.An chorStyles.Bott om Or
System.Windows. Forms.AnchorSty les.Left), System.Windows. Forms.AnchorSty les)
Me.lblThreadCou nt.Location = New System.Drawing. Point(8, 347)
Me.lblThreadCou nt.Name = "lblThreadCount "
Me.lblThreadCou nt.TabIndex = 11
Me.lblThreadCou nt.Text = "0"
'
'lblTotal
'
Me.lblTotal.Anc hor = CType((System.W indows.Forms.An chorStyles.Bott om
Or System.Windows. Forms.AnchorSty les.Right),
System.Windows. Forms.AnchorSty les)
Me.lblTotal.Loc ation = New System.Drawing. Point(280, 347)
Me.lblTotal.Nam e = "lblTotal"
Me.lblTotal.Siz e = New System.Drawing. Size(48, 23)
Me.lblTotal.Tab Index = 10
Me.lblTotal.Tex t = "0"
'
'Label2
'
Me.Label2.Ancho r = CType((System.W indows.Forms.An chorStyles.Bott om
Or System.Windows. Forms.AnchorSty les.Right),
System.Windows. Forms.AnchorSty les)
Me.Label2.Locat ion = New System.Drawing. Point(240, 347)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing. Size(24, 23)
Me.Label2.TabIn dex = 9
Me.Label2.Text = "Of"
'
'lblFinished
'
Me.lblFinished. Anchor =
CType((System.W indows.Forms.An chorStyles.Bott om Or
System.Windows. Forms.AnchorSty les.Right), System.Windows. Forms.AnchorSty les)
Me.lblFinished. Location = New System.Drawing. Point(192, 347)
Me.lblFinished. Name = "lblFinishe d"
Me.lblFinished. Size = New System.Drawing. Size(32, 23)
Me.lblFinished. TabIndex = 8
Me.lblFinished. Text = "0"
'
'btnGo
'
Me.btnGo.Anchor = CType((System.W indows.Forms.An chorStyles.Bott om Or
System.Windows. Forms.AnchorSty les.Right), System.Windows. Forms.AnchorSty les)
Me.btnGo.Locati on = New System.Drawing. Point(336, 347)
Me.btnGo.Name = "btnGo"
Me.btnGo.TabInd ex = 7
Me.btnGo.Text = "Go"
'
'dgMailboxes
'
Me.dgMailboxes. Anchor =
CType((((System .Windows.Forms. AnchorStyles.To p Or
System.Windows. Forms.AnchorSty les.Bottom) _
Or System.Windows. Forms.AnchorSty les.Left) _
Or System.Windows. Forms.AnchorSty les.Right),
System.Windows. Forms.AnchorSty les)
Me.dgMailboxes. DataMember = ""
Me.dgMailboxes. HeaderForeColor =
System.Drawing. SystemColors.Co ntrolText
Me.dgMailboxes. Location = New System.Drawing. Point(0, 3)
Me.dgMailboxes. Name = "dgMailboxe s"
Me.dgMailboxes. Size = New System.Drawing. Size(416, 328)
Me.dgMailboxes. TabIndex = 6
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(416, 373)
Me.Controls.Add (Me.lblThreadCo unt)
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.dgMail boxes,
System.Componen tModel.ISupport Initialize).End Init()
Me.ResumeLayout (False)
End Sub
#End Region
Private m_dt As DataTable
Private LastIndex As Integer
Private IndexQueue As Queue = Queue.Synchroni zed(New Queue)
Private m_CurrentRowInd ex As Integer
Private ThreadCount As Integer
Private FinishedCount As Integer
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Create and show a datatable of items to be processed and track
progress
m_dt = New DataTable
m_dt.Columns.Ad d("Index")
m_dt.Columns.Ad d("Delay")
m_dt.Columns.Ad d("ThreadState" )
m_dt.Columns.Ad d("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(N ewRow)
Next
dgMailboxes.Dat aSource = m_dt
End Sub
Private Sub btnGo_Click(ByV al sender As System.Object, ByVal e As
System.EventArg s) 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_CurrentRowInd ex = 0 To LastIndex
IndexQueue.Enqu eue(m_CurrentRo wIndex)
Next
'Process items in the queue
While IndexQueue.Coun t > 0
lblThreadCount. Text = CStr(ThreadCoun t)
lblThreadCount. Refresh()
'Launch a new thread if there are less than 20 threads running
If ThreadCount < 20 Then
Dim t As New Thread(AddressO f RunDummyTask)
t.IsBackground = False
t.Start()
End If
'Refresh Gui objects
dgMailboxes.Ref resh()
lblTotal.Refres h()
lblThreadCount. Refresh()
'needed while threads are starting
lblFinished.Tex t = CStr(FinishedCo unt)
lblFinished.Ref resh()
'Program crashes without this line. Why?
'Error: Queue Empty
Thread.CurrentT hread.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.CurrentT hread.Sleep(Del ay)
'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.Tex t = CStr(FinishedCo unt)
'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