473,382 Members | 1,431 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,382 software developers and data experts.

Control number of BackgroundWorkers?

I have a small program which has a large (~50K) number of objects which need
to do work.

I create a BackgroundWorker object and fire it off whenever each of the
instances needs to do work; this all works fine, but I'm not sure if I'm
using the class correctly.

Should have an internal limit on the number of BackgroundWorkers I fire off,
or does the fact that it internally uses the ThreadPool anyway mean that I
don't need to bother?

--
Paul
Nov 17 '08 #1
1 3606
On Mon, 17 Nov 2008 02:20:01 -0800, Paul Hatcher
<Pa*********@discussions.microsoft.comwrote:
I have a small program which has a large (~50K) number of objects which
need
to do work.

I create a BackgroundWorker object and fire it off whenever each of the
instances needs to do work; this all works fine, but I'm not sure if I'm
using the class correctly.

Should have an internal limit on the number of BackgroundWorkers I fire
off,
or does the fact that it internally uses the ThreadPool anyway mean that
I
don't need to bother?
It depends on what your objectives are, as well as the specifics of the
activity of your objects (how often they start a new task, how long that
task takes, and whether the task is CPU-bound or not).

The thread pool adds a new thread every half second as long as there are
queued tasks waiting. If your tasks take that long or longer, or you are
queueing a large number of them at once, the thread pool could wind up
allocating a very large number of threads. If those threads are
CPU-bound, then that could indeed cause undue contention for the CPU.

If your tasks are not CPU-bound, or they take significantly less than a
half-second to complete and you won't have a lot of them active at once,
then using the thread pool (via BackgroundWorker or otherwise) is probably
fine. Otherwise, you may want to create your own work queue that you can
throttle, so as to avoid CPU contention.

Note that doesn't mean you have to forego using the thread pool...it just
means limiting the number of tasks concurrently assigned to the thread
pool at once. In fact, one possible approach would be to have the
RunWorkerCompleted event handler for the BackgroundWorker check your queue
and if there's something still to be executed, restart that same
BackgroundWorker with the new work item, avoiding the need to churn
BackgroundWorker instances.

Pete
Nov 17 '08 #2

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

Similar topics

5
by: Cro | last post by:
Hello Access Developers, I'd like to know if it is possible to perform a count in an expression that defines a control source. My report is based on a query. In my report, I want a text box to...
5
by: Mike | last post by:
I have a form that has a control on it. When data is entered into the control I verify that is good with the follwoing code: ==> Begin Code <== Dim ct, curbidnum curbidnum = Me.biddernum ...
0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
15
by: Tinus | last post by:
Hello all, I've created a custom control that draws a monthly schedule (using the Draw function in C#). Basically it draws 31 boxes and writes the day number in every box. This works...
3
by: hans.werner | last post by:
hi, I have a little problem with a custom control and for each (number is an integer) (the control names are AxCTC1 etc Dim ctrCTI As AxCTCLib.AxCtc For Each ctrCTI In Me.Controls <----...
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
0
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This will add client-side event handlers for most of the...
6
by: MarcuEusebiu | last post by:
Hello, I have a Queue and 2 BackGroundWorkers. How can I set one worker to Peek in Queue and the other one to Dequeue the first element from queue? Thanks
2
by: Mike B | last post by:
I'm a bit of a threading newbie, and have just converted an app from Threads to BackgroundWorkers to try to get some more life out of the UI. From reading here and elsewhere, I have a general...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.