Control number of BackgroundWorkers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGF1bCBIYXRjaGVy?=

    #1

    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 BackgroundWorke r 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 BackgroundWorke rs I fire off,
    or does the fact that it internally uses the ThreadPool anyway mean that I
    don't need to bother?

    --
    Paul
  • Peter Duniho

    #2
    Re: Control number of BackgroundWorke rs?

    On Mon, 17 Nov 2008 02:20:01 -0800, Paul Hatcher
    <PaulHatcher@di scussions.micro soft.comwrote:
    I have a small program which has a large (~50K) number of objects which
    need
    to do work.
    >
    I create a BackgroundWorke r 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 BackgroundWorke rs 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 BackgroundWorke r 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
    RunWorkerComple ted event handler for the BackgroundWorke r check your queue
    and if there's something still to be executed, restart that same
    BackgroundWorke r with the new work item, avoiding the need to churn
    BackgroundWorke r instances.

    Pete

    Comment

    Working...