468,505 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

question on using collection - Queue and Threading

Hi all,
I got a question here which about using queue. i have create a class of queue.

Public Sub Add(ByVal data As Object)
SyncLock Me
_Data.Enqueue(data)
Monitor.Pulse(Me)
End SyncLock
End Sub
Public Function GetNext() As Object
SyncLock Me
While _Data.Count = 0
Monitor.Wait(Me)
End While
Return _Data.Dequeue()
End SyncLock
End Function

above are part of the class of queue. I am going to start a thread to add data to the queue. At the same time, another thread are also start to get data.
if the queue is empty, the thread will wait until the other thread (add) add data into queue.
My problem is if there is no more data to add into queue, the thread (get) is still waiting. I would like to ask how can I add WaitForCompletion to handle it.
Nov 16 '05 #1
3 1629
kelkel <ke****@discussions.microsoft.com> wrote:
I got a question here which about using queue. i have create a class of queue.

Public Sub Add(ByVal data As Object)
SyncLock Me
_Data.Enqueue(data)
Monitor.Pulse(Me)
End SyncLock
End Sub
Public Function GetNext() As Object
SyncLock Me
While _Data.Count = 0
Monitor.Wait(Me)
End While
Return _Data.Dequeue()
End SyncLock
End Function

above are part of the class of queue. I am going to start a thread to
add data to the queue. At the same time, another thread are also
start to get data.
if the queue is empty, the thread will wait until the other thread
(add) add data into queue.
My problem is if there is no more data to add into queue, the thread
(get) is still waiting. I would like to ask how can I add
WaitForCompletion to handle it.


Well, you could have another method, "Stop" or similar which set a flag
and pulsed the monitor. Your while loop in GetNext would then test the
flag on each iteration, and return Nothing if it was set.

By the way, I'd suggest avoiding locking on Me. See
http://www.pobox.com/~skeet/csharp/m...ml#lock.choice for
reasons.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Thx Jon,
I understand what u have said but could you please let me know where the flag should I put?
"Jon Skeet [C# MVP]" wrote:
kelkel <ke****@discussions.microsoft.com> wrote:
I got a question here which about using queue. i have create a class of queue.

Public Sub Add(ByVal data As Object)
SyncLock Me
_Data.Enqueue(data)
Monitor.Pulse(Me)
End SyncLock
End Sub
Public Function GetNext() As Object
SyncLock Me
While _Data.Count = 0
Monitor.Wait(Me)
End While
Return _Data.Dequeue()
End SyncLock
End Function

above are part of the class of queue. I am going to start a thread to
add data to the queue. At the same time, another thread are also
start to get data.
if the queue is empty, the thread will wait until the other thread
(add) add data into queue.
My problem is if there is no more data to add into queue, the thread
(get) is still waiting. I would like to ask how can I add
WaitForCompletion to handle it.


Well, you could have another method, "Stop" or similar which set a flag
and pulsed the monitor. Your while loop in GetNext would then test the
flag on each iteration, and return Nothing if it was set.

By the way, I'd suggest avoiding locking on Me. See
http://www.pobox.com/~skeet/csharp/m...ml#lock.choice for
reasons.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
kelkel <ke****@discussions.microsoft.com> wrote:
I understand what u have said but could you please let me know where
the flag should I put?


Somewhere in the class. Just make it an instance variable.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andrew Gregory | last post: by
9 posts views Thread by phil | last post: by
17 posts views Thread by Arun Kumar | last post: by
18 posts views Thread by Nick Z. | last post: by
2 posts views Thread by www.brook | last post: by
6 posts views Thread by fniles | last post: by
19 posts views Thread by frankiespark | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.