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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:28:14+00:00 2026-06-09T10:28:14+00:00

I have a Task factory thats kicking off many tasks, sometimes over 1000. I

  • 0

I have a Task factory thats kicking off many tasks, sometimes over 1000. I add every Task to a list, and remove it when the Task has completed.

var scan = Task.Factory.StartNew(() =>
            {
                return operation.Run();
            }, token.Token
            );

            operations.Add(scan);

When a task Completes:

var finishedTask = scan.ContinueWith(resultTask =>
                OperationComplete(resultTask),
                TaskContinuationOptions.OnlyOnRanToCompletion
                );

public virtual void OperationComplete(Task task)
    {
        operations.Remove(task);
    }

When all are complete:

    Task.Factory.ContinueWhenAll(operations.ToArray(),
         result =>
         {
             AllOperationsComplete();
         }, TaskContinuationOptions.None);

Then, at certain points in my application I want to get the count of running tasks. (This is where I get the error: “Collection was modified; enumeration operation may not execute.”)

    public int Count()
    {  
        int running = operations.Count<Task>((x) => x.Status == TaskStatus.Running);
        return running;
    }

A couple questions:

1) Should I even worry about removing the tasks from the list? The list could easily be in the 1000s.

2) Whats the best way to make Count() safe? Creating a new List and adding operations to it will still enumerate the collection, if I remember right.

  • 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-09T10:28:16+00:00Added an answer on June 9, 2026 at 10:28 am

    Either you need to lock to make sure only one thread accesses the list at a time (whether that’s during removal or counting) or you should use a concurrent collection. Don’t forget that Count(Func<T, bool>) needs to iterate over the collection in order to perform the count – it’s like using a foreach loop… and you can’t modify a collection (in general) while you’re iterating over it.

    I suspect that ConcurrentBag is an appropriate choice here – and as you’re using TPL, presumably you have the .NET 4 concurrent collections available…

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

Sidebar

Related Questions

I have this code: Dim Tasks As New List(Of Task) Tasks.Add(Task.Factory.StartNew(Sub() 'Do whatever End
In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
In my ViewModel I have this code: Logs = new ObservableCollection<Log>(); Logs = Task.Factory.StartNew(()
Let's say I have a collection of System.Threading.Tasks.Task : HashSet<Task> myTasks = new HashSet<Task>();
I have a task that launches several child tasks. (e.g., Task A creates B,C,D,E,F).
i have task which takes a parameter and has three modes of results Example
I have task involving reading SAS .xpt files using .NET. For that I'm using
I am new this sharepoint development and i have task in hand to do

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.