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 8054835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:14:13+00:00 2026-06-05T08:14:13+00:00

Apologies if this is simple question, but Im extremely worn down and though thinking

  • 0

Apologies if this is simple question, but Im extremely worn down and though thinking straight. I recently set up threading and its working great. The user selects items to have work performed on, and I dynamicaly create an array of Threads for however many items they select and dispatch one thread per item.

However, I noticed that if too many are selected the server the files are on doesnt play nice. I know for a fact that I can declare only 5 active threads a time, even if there are 30 items that need work, but I can’t wrap my head around it.

PS: If Im describing ThreadPool let me know… Ive tried to get that to work but had a hell of a time and no luck, pretty new to .NET way of threading.

Current way:

threadedResults = new List<string>[SelectedItems.Count];
List<string> results = new List<string>();
ThreadSettings tsArgs = (ThreadSettings)args;

for (int i = 0; i < SelectedItems.Count; i++)
{
    threadedResults[i] = new List<string>();
}

Thread[] threads = new Thread[SelectedItems.Count];

for (int i = 0; i < SelectedItems.Count; i++)
{
    KeyValuePair<int, ThreadSettings> threadArgs = new KeyValuePair<int, ThreadSettings>(i, (ThreadSettings)tsArgs);

    threads[i] = new Thread(new ParameterizedThreadStart(DoWork));
    threads[i].Start(threadArgs);
}
for (int i = 0; i < SelectedItems.Count; i++)
{
    if (CommandTask != null)
        threads[i].Join();
    else
    {
        // User Cancelled, abort all threads and break
        for (int j = 0; j < SelectedItems.Count; j++)
        {
            threads[j].Abort();
        }
        break;
    }
}

// Gather all results, format and return
  • 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-06-05T08:14:15+00:00Added an answer on June 5, 2026 at 8:14 am

    Basically, you’d want to do something like this in the TPL:

            // source for cancelling work
            var cxlSource = new CancellationTokenSource();
    
            // create and schedule tasks to start
            var tasks =
                Enumerable.Range(0, SelectedItems.Count)
                    .Select(i => Task.Factory.StartNew(DoWork, yourStateInfo, cxlSource.Token))
                    .ToArray();
    
            // then wait for the results
            Task.WaitAll(tasks);
    

    If at any time, you need to cancel the tasks, you can request a cancel:

                cxlSource.Cancel();
    

    Which will prevent any tasks awaiting execution from starting, and your tasks can examine the token themselves periodically so they can abort early if cancelled while in progress.

    There’s a lot of nuance to using the TPL (and threading in general), but that’s a rough idea.

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

Sidebar

Related Questions

Apologies if this is a simple question but googling hasn't been able to help
Apologies if this is a simple question, I'm still pretty new to this, but
Apologies if this is an overly simple question, but my searches are getting me
Firstly apologies if this is a really simple question but Git is absolutely brand
Apologies if this is a simple question, but I turn to the wisdom of
Apologies if this is a slightly noob question, but looking to clarify my thoughts
Apologies if this is a dumb question but I'm trying to format a currency
Apologies, this is a tragically simple question that will bore most of you. I
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
This is extremely basic and I apologize for asking such a rookie question. But

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.