Producer-Consumer threading problem using vb.net

  • Thread starter Thread starter Ramta
  • Start date Start date
R

Ramta

Hi all,

I am trying to develop a Producer thread that listens for UDP packets on a
socket and store then in an ArrayList.

The consumer should read data from list and block when no element is in
list.

When producer inserts a message in list, it should make the consumer aware
of the item and consumer should process the item as soon as it is available.

How can i do this using vb.net

Thanks in advance
 
Ramta,
Rather then use an ArrayList for the packets received I would recommend a
Queue as its FIFO.

I normally use an AutoResetEvent to allow the Producer to notify the
Consumer that a new item is in the Queue.

Normally I put the Thread, the AutoResetEvent, the Queue and the padlock for
the Queue into a single class that represents the "Worker" (your Consumer).
Encapsulating the above into clean type safe methods.

Something like:

' untested, typed from memory.
Public Class ThreadRequestQueue

Private Readonly m_padlock As New Object
Private Readonly m_queue As New Queue
Private Readonly m_event As New AutoResetEvent(False)

Public Sub AddRequest(ByVal request As Object)
SyncLock m_padlock
m_queue.Enqueue(Object)
End SyncLock
m_event.Set()
End Sub

Public Function GetRequest() As Object
' Check to see if there are already items available
SyncLock m_padlock
If m_queue.Count() > 0 Then
Return m_queue.DeQueue()
End If
End SyncLock

' Cannot block main thread
' while waiting for the main thread to add requests
' hence no SyncLock here
m_event.WaitOne()

' There must be an item
SyncLock m_padlock
Return m_queue.Dequeue()
End SyncLock
End Function

End Class

Hope this helps
Jay


| Hi all,
|
| I am trying to develop a Producer thread that listens for UDP packets on a
| socket and store then in an ArrayList.
|
| The consumer should read data from list and block when no element is in
| list.
|
| When producer inserts a message in list, it should make the consumer aware
| of the item and consumer should process the item as soon as it is
available.
|
| How can i do this using vb.net
|
| Thanks in advance
|
|
 
Ramta,

I made a sample with a timer for this guestion a while ago.
However I made a new one, this one shows it maybe nicer.
(Although I changed the old one in a very lazy way)

\\\Needs A form with a listbox and this code
Private myQ As New Queue
Private myreadDb1 As New readData(myQ, 1)
Private myreadDb2 As New readData(myQ, 2)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyThr1 As New System.Threading.Thread(AddressOf myreadDb1.Read)
AddHandler myreadDb1.IgotIt, AddressOf Me.myreadDb_IgotIt
MyThr1.Start()
Dim MyThr2 As New System.Threading.Thread(AddressOf myreadDb2.Read)
MyThr2.Start()
AddHandler myreadDb2.IgotIt, AddressOf Me.myreadDb_IgotIt
End Sub
Private Sub myreadDb_IgotIt()
Do Until myQ.Count = 0
SyncLock myQ.SyncRoot
If myQ.Count > 0 Then
Me.ListBox1.Items.Add(myQ.Dequeue)
Me.ListBox1.SelectedIndex = ListBox1.Items.Count - 1
Me.ListBox1.Show()
End If
End SyncLock
Loop
End Sub
End Class
////
\\\
Public Class readData
Private MyQ As Queue
Private Int As Integer
Public Event IgotIt()
Public Sub New(ByVal pMyQ As Queue, ByVal MInt As Integer)
MyQ = pMyQ
Int = MInt
End Sub
Friend Sub Read()
For i As Integer = 65 To 90
SyncLock MyQ.SyncRoot
If Int = 1 Then
MyQ.Enqueue(i - 65)
Else
MyQ.Enqueue(ChrW(i))
End If
End SyncLock
Threading.Thread.Sleep(CInt(i * Int * 10))
RaiseEvent IgotIt()
Next
End Sub
End Class
///

I hope this helps a little bit?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top