tags:

views:

340

answers:

3

I have this timer function, it gives me following exception.
Collection was modified; enumeration operation may not execute once i remove the object from hashtable.

what is the solution to implement similar functionality

void timerFunction(object data)
    {
    lock (tMap.SyncRoot)
    {
      foreach (UInt32 id in tMap.Keys)
      {
         MyObj obj=(MyObj) runScriptMap[id];
         obj.time = obj.time -1;
         if (obj.time <= 0)
         {                      
            tMap.Remove(id);
         }
       }
    }
+6  A: 

The traditional way is, in your foreach loop, to collect the items to be deleted into, say, a List. Then once the foreach loop has finished, do another foreach over that List (not over tMap this time), calling tMap.Remove:

void timerFunction(object data)
{
  lock (tMap.SyncRoot)
  {
    List<UInt32> toRemove = new List<UInt32>();

    foreach (UInt32 id in tMap.Keys)
    {
      MyObj obj=(MyObj) runScriptMap[id];
      obj.time = obj.time -1;
      if (obj.time <= 0)
      {                      
        toRemove.Add(id);
      }
    }

    foreach (UInt32 id in toRemove)
    {
      tMap.Remove(id);
    }
  }
}
itowlson
It's worth pointing out that the thread safety isn't really a factor here. You'd get the same error regardless of whether one thread or multiple threads were meddling with your collection mid-enumeration. (Though of course I can see why you want to restrict it to a single thread.)
sgreeve
+1  A: 

http://social.msdn.microsoft.com/Forums/en-IE/csharplanguage/thread/519c6abc-5b5b-457a-adfb-251ee013d123

     List<string> tablesNamesToRemove = new List<string>();
     foreach (string ikey in gridNum.Keys)
     {
        Hashtable aux = (Hashtable)gridNum[ikey];
        if (aux["OK"].ToString().Trim() == "1")
           tablesNamesToRemove.Add(ikey);
     }

     foreach (string name in tablesNamesToRemove)
     {
        gridNum.Remove(name);
     }
Kazoom
+1  A: 

You could make a temporary list, and add the Ids to remove to that list. Then after your loop finishes, remove all items in that list:

void timerFunction(object data)
    {
    lock (tMap.SyncRoot)
    {
      IList<UInt32> toDelete = new List<UInt32>();
      foreach (UInt32 id in tMap.Keys)
      {

         MyObj obj=(MyObj) runScriptMap[id];
         obj.time = obj.time -1;
         if (obj.time <= 0)
         {                      
            toDelete.add(id);
         }
       }
      foreach(UInt32 i in toDelete)
      {
          tMap.Remove(i);
      }
    }
adharris