tags:

views:

285

answers:

3

I'm working on my first ThreadPool application in Visual Studio 2008 with C#.

I have a report that has to perform calculations on 2000 to 4000 parts using data on our SQL Server.

I am Queuing all of the part numbers in a ThreadPool, where they go off and calculate their results. When these threads are finished, the RegisterWaitForSingleObject event fires to Unregister the Queued Item's Handle.

After all of the Queued Items have finished, is there a way to remove them from the ThreadPool?

The way it looks, if someone runs another report using a new set of 2000 to 4000 parts, I have no way of removing the previous array of parts.

How would I remove the Previously Queued Items? Would calling SetMaxThreads with workerThreads = 0 do it?

I realize I could experiment, but then I could waste most of the week experimenting.

Thanks for your time,
Joe

+2  A: 

Once a ThreadPool item completes, it is automatically removed from the queue. What is indicating to you that they aren't?

Adam Robinson
Nothing indicates they are not ...but at the same time, nothing indicates they are! I wasn't able to find any documentation that said old items entered into the Queue were *ever* released.Thanks for your answer! Joe
jp2code
+2  A: 

Assuming you mean to interrupt (cancel) the work on the current queue...

Changing the max-threads won't affect the pending work; it'll just change the number of threads available to do it - and it is generally a bad idea to mess with this (your code isn't the only thing using the ThreadPool). I would use a custom queue - it is fairly easy to write a basic (thread-safe) producer/consumer queue, or .NET 4.0 includes some very good custom thread queues.

Then you can just abort the custom queue and start a new one.

I wrote a simple one here; at the moment it wants to exit cleanly (i.e. drain the queue until it is empty) before terminating, but it would be easy enough to add a flag to stop immediately after the current item (don't resort to interrupting/aborting threads at an arbitrary point in execution; never a good idea).

Marc Gravell
Thanks for the help, Mr. Gravell. I will say that I *have* already run GetMaxThreads(out workerThreads, out completionPortThreads), doubled both values, and then tested saving them with the call to ThreadPool.SetMaxThreads(workerThreads, completionPortThreads). How long is that setting going to remain active, just while the parent process is active, until I reboot, or until I call SetMaxThreads again with another value?
jp2code
Per process or until you change it again, I suspect.
Marc Gravell
A: 

I received an email from Stack Overflow about 3 hours ago (timestamp Wed, Sep 23, 2009 at 5:01 AM), and I have my profile set to notify me daily of any new answers.

I don't see any dates above other than some comments that I made, and they only say "yesterday". I know I made those comments on Friday (Sep 18, 2009) only a few short hours after I posted the question and received my two replies.

Why does it take so long to get a reply?

Why doesn't Stack Overflow show dates?

jp2code