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

  • Home
  • SEARCH
  • 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 6681139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:23+00:00 2026-05-26T04:33:23+00:00

I need help. I am working with an arraylist, and suddenly I get this

  • 0

I need help. I am working with an arraylist, and suddenly I get this error.

An unhandled exception of type ‘System.InvalidOperationException’ occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation may not execute.

This is the code where it shows the exception…

foreach (PC_list x in onlinelist) {
  if ((nowtime.Subtract(x.time)).TotalSeconds > 5) {
    Invoke(new MethodInvoker(delegate {
      index = Main_ListBox.FindString(x.PcName);
      if(index != ListBox.NoMatches)
      Main_ListBox.Items.RemoveAt(index);
    }));
    onlinelist.Remove(x);
    //Thread.Sleep(500);
  }
}

Where

public class PC_list {
    public string PcName;
    public string ip;
    public string status;
    public string NickName;
    public DateTime time;

}

Notes:

  • onlinelist is an arraylist
  • nowtime and x.time are DateTime.

Call Stack

mscorlib.dll!System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() + 0x122 bytes    
BlueBall.exe!BlueBall.BlueBall.clean_arraylist() Line 74 + 0x1a8 bytes  C#
BlueBall.exe!BlueBall.BlueBall.server() Line 61 + 0x8 bytes C#
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
[Native to Managed Transition]
  • 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-26T04:33:23+00:00Added an answer on May 26, 2026 at 4:33 am
    foreach (PC_list x in onlinelist)
    {            
         onlinelist.Remove(x); // cannot do this
    }
    

    This is the heart of the problem. You cannot remove an item from a collection as you are iterating over it in a foreach. Your options are to make a local copy of the list prior to the loop, loop over the copy, and remove from the original. Or you can keep a separate list of items to remove after you finish the original loop. Or you can switch to a for loop and iterate over it backwards, which allows you to remove items from the end as you go.

    While you’re here, if you are not stuck working with C# 1 / .NET 1.1 / Visual Studio 2003, you might want to consider switching from ArrayList to the stronger List<T>, where T is the type of the object in the collection. In your case, that would be a List<PC_list>. You can find it at System.Collections.Generic.List<T>.

    And since your question is tagged multithreading, it would also be a smart idea to consult the collections built with concurrency in mind.

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

Sidebar

Related Questions

Hey Guys...need help. Working on a project and get this error on the Output
Ok, I need help. This is my first question here. Background: I am working
I need help with this batch file I'm working on, Basically everytime it does
I need some help with re-working the logic on this php code. What I'd
Need help here. i managed to get the drinkName and categories working. http://dl.dropbox.com/u/418769/2.png but
I need help to get ADB working on my PC (win7 64bit) with the
I'm new to Java, and I need some help working on this program. This
I need help. I am struggling to get my Observers working in java. Can
I need help working with very big numbers. According to Windows calc, the exponent
i have a problem, i'm working on a gallery php script and need help,

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.