Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7690043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:18:24+00:00 2026-05-31T20:18:24+00:00

I have a loop which needs to create an unspecified and indefinite amount of

  • 0

I have a loop which needs to create an unspecified and indefinite amount of Background threads. The threads will then simply run to completion but some may need to be cancelled.

I originally planned to do this by declaring one backgroundWorker, adding it to a list, then newing the BackgroundWorker to begin the process again. However because the List.Add method runs by reference to the passed in object I am thinking that when I new the original BackgroundWorker, the thread referenced by the List will be lost (or worse).

My second thought was simply assigning my newed value to a element in an array (array[i] = new BackgroundWorker()) but this does not lend itself to an indefinite amount of threads.

Does anyone have any suggestions of a good way to manage these individual threads? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:18:25+00:00Added an answer on May 31, 2026 at 8:18 pm

    Two comments – one micro, one macro.

    First, it seems like the TPL will fit your stated goals. Using Task.Factory.StartNew(), you’ll be able to spawn an arbitrary amount of operations which will run on background threads. Further, using the overloads that take a CancellationToken, you cancel operations to as fine a degree as you might wish. Note that a Task is not equivalent to a Thread – by default, Tasks run in ThreadPool threads (as do background workers, by the way). It’s entirely possible that your tasks will end up in a queue waiting for a pooled thread to become available.

    Second, the bigger comment.

    You claim that you will have an unspecified and indefinite number of work items. That you would spawn a thread per item makes me a bit edgy – threads aren’t cheap, and if you have a large number of work items you can starve your process by naively starting new threads.

    Can you elaborate on what you need to do with these threads? I don’t know what constitutes a work item for your project, but it’s entirely possible that a producer/consumer setup with a fixed number of threads would meet your parallelization needs. .NET 4.0 makes it easy with the System.Collections.Concurrent classes.

    EDIT in response to comment

    The case of TCP connections is very nearly what I was thinking about when I wrote the second comment above. In this case, you are considering a potentially infinite consumption of a limited OS resource, namely open sockets. I hope that you will investigate non-blocking I/O. This MSDN page documents asynchronous reading from sockets, and this one talks about the larger asynchronous programming model in .NET.

    For scalable TCP serving, a good discussion already exists on SO: peruse at your pleasure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which needs to loop through all the lines in text
I have a loop MC which will be duplicate to stage several times according
I have the following command which will loop over all the subdirectories in a
I have a loop which basically calls this every few seconds (after the timeout):
I have a for-loop which performs the following function: Take a M by 8
i have a php loop which displays only one record even if there is
If I have a for loop which is nested within another, how can I
I have a binary search loop which gets hit many times in the execution
I have some code which collects points (consed integers) from a loop which looks
I have the following loop inside an function init(); which is executed onload, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.