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(d ata)
Monitor.Pulse(M e)
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 WaitForCompleti on to handle it. 3 1792
kelkel <ke****@discuss ions.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(d ata) Monitor.Pulse(M e) 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 WaitForCompleti on 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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****@discuss ions.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(d ata) Monitor.Pulse(M e) 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 WaitForCompleti on 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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
kelkel <ke****@discuss ions.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew Gregory |
last post by:
I've written a Tkinter application (Windows) that uses the threading
module, which I've reduced to the test case below (apologies if it's a
bit long). Using the Queue module to pass messages the callable
function in ClassDoRun can be started and interrupted via 'Start' and
'Abort' buttons. Having started then interrupted the function I
expected to be able to execute the function again from the beginning
using the 'Start' button - but I get...
|
by: phil |
last post by:
And sorry I got ticked, frustrating week
>And I could help more, being fairly experienced with
>threading issues and race conditions and such, but
>as I tried to indicate in the first place, you've
>provided next to no useful (IMHO) information to
>let anyone help you more than this
This is about 5% of the code.
Uses no locks.
|
by: m |
last post by:
Hello,
I have an application that processes thousands of files each day. The
filenames and various related file information is retrieved, related
filenames are associate and placed in a linked list within a single object,
which is then placed on a stack(This cuts down thread creation and deletions
roughly by a factor of 4). I create up to 12 threads, which then process a
single object off of the stack. I use a loop with a boolean...
|
by: Arun Kumar |
last post by:
What is wrong with this code. All i am trying to test is 3 progressbar and
one button. On buttonclick i create 3 threads and each thread calls a method
which in turn updates the progressbar and it works. I would to know if this
can be used.
Thanks
private void button1_Click(object sender, System.EventArgs e)
{
ThreadStart job = new ThreadStart(onemethod);
|
by: Nick Z. |
last post by:
I am writing a reusable class for logging.
My goal is to make it as fast and as robust as possible, while keeping
memory usage to the lowest. All members are static.
For some reason I'm stuck on the following design question.
Obviously you need a stream to write the log file.
Should this stream be created every time the log needs to be written, or
should the stream be a member variable of the logging class and only be
opened and closed...
| |
by: www.brook |
last post by:
I have a VC++.net code. Two threads are created, one thread keeps
appending elements to the queue, another keeps deleting an element from
the queue.
I tried to use monitor, but it seems that the there is problem. in the
thread of the deleting, seems that the deleting function entered twice
and both
try to delete the queue. The first thread find it is not empty, and try
to delete an element, but before this thread actually delete it,...
|
by: admin |
last post by:
ok This is my main. Pretty much it goes through each category and
starts up 4 worker threads that then ask for groups to gether from. My
problem is that when the thread gets done it keeps the mysql
connections open so I end up with quite a few at the end. Is there a
different way that I should do this?
class Program
{
static string categories = { "emulation" , "audio" ,
"console" , "anime" , "xxx" , "tv" , "pictures" , "video" };
|
by: fniles |
last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to
clients.
As I receive data in 1 thread, I put it into an arraylist, and then I remove
the data from arraylist and send it to the client.
Before adding data to the arraylist, I check if the depth of the arraylist
is longer than iMaxQueueDepth, and if it is, I clear the arraylist.
Is it possible that while I am clearing the arraylist, the ThreadMain at the
same time...
|
by: frankiespark |
last post by:
Hello all,
I was perusing the internet for information on threading when I came
across this group. Since there seems to be a lot of good ideas and
useful info I thought I'd pose a question.
Threading is a new concept for me to implement. Here is my problem.
I have a system that receives xml files and records their file
locations in a database. I can potentially receive thousands,
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |