471,059 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,059 software developers and data experts.

Thread Safe/Synchronized Queue question

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()

'This call is required by the Windows Form Designer.

'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
End If
End If
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
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
System.ComponentModel.ISupportInitialize).BeginIni t()
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"
Me.lblTotal.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right),
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"
Me.Label2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom
Or System.Windows.Forms.AnchorStyles.Right),
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"
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"
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"
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),
Me.dgMailboxes.DataMember = ""
Me.dgMailboxes.HeaderForeColor =
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
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 373)
Me.Name = "Form1"
Me.Text = "Form1"
System.ComponentModel.ISupportInitialize).EndInit( )

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
m_dt = New DataTable

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

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

'Process items in the queue
While IndexQueue.Count > 0
lblThreadCount.Text = CStr(ThreadCount)

'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
End If

'Refresh Gui objects
'needed while threads are starting
lblFinished.Text = CStr(FinishedCount)

'Program crashes without this line. Why?
'Error: Queue Empty

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

'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
End If

End Sub

Private Sub Done()
End Sub

End Class

Nov 21 '05 #1
0 1519

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by sayoyo | last post: by
reply views Thread by Chris Stransky | last post: by
1 post views Thread by Frank Rizzo | last post: by
2 posts views Thread by Shane Story | last post: by
6 posts views Thread by fniles | last post: by
9 posts views Thread by cgwalters | last post: by
29 posts views Thread by NvrBst | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.