473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Producer-Consumer threading problem using vb.net

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
Nov 21 '05 #1
2 4191
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
"Ramta" <gu***********@msn.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl...
| 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
|
|
Nov 21 '05 #2
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
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wai Yip Tung | last post by:
I'm attempting to turn some process than uses callback to return result into a more user friendly generator. I'm hitting some road block so any pointer would be appriciated. Let say there is an...
2
by: mag31 | last post by:
This is a technically challenging question but it would help me significantly if anyone can answer it. In essence I have the following: A factory class, which can create producer classes which...
3
by: Vaddina Prakash Rao | last post by:
Hello every one, In our Linux Encoding system, a copy of helix dna producer is running ..... i wrote an applicatoin which can remotely communicate with the encoding machine ... Its function...
3
by: Vern | last post by:
The following code retrieves data into a dataset, and then creates a dataview with a filter. This dataview is then attached to a combobox. When the effective date changes, I would like to see the...
1
by: Jakub Okaj | last post by:
I would like to use ASP like PageProducer in Delphi CGI aplication. How can I do this? Please help. QBA
5
by: cozsmin | last post by:
hello , as u know wait() and notify() will not thow an exception if the method that calls them has the lock , or esle i misundrestood java :P this is the code that throws (unwanted) ...
2
by: dwhall | last post by:
I have 2 python scripts: examples of a producer and a filter, respectively: #! /usr/bin/env python import sys, time if __name__ == "__main__": while True: sys.stdout.write("hello.\r\n")...
12
by: test63 | last post by:
with which program, i can make a 3d game(like world of warcraft, alladin.....)? chears-
4
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.