集合已修改;哈希表可能无法执行枚举操作

发布于 2024-08-23 20:19:48 字数 423 浏览 3 评论 0原文

我有这个计时器功能,它给了我以下异常。
集合已修改;枚举操作可能无法执行 一旦我从哈希表中删除对象。

实现类似功能的解决方案是什么

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);
         }
       }
    }

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);
         }
       }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白衬杉格子梦 2024-08-30 20:19:48

传统的方法是,在 foreach 循环中,将要删除的项目收集到列表中。然后,一旦 foreach 循环完成,就对该 List 执行另一个 foreach(这次不是在 tMap 上),调用 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);
    }
  }
}

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);
    }
  }
}
终陌 2024-08-30 20:19:48

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);
     }

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);
     }
风情万种。 2024-08-30 20:19:48

您可以创建一个临时列表,并将要删除的 ID 添加到该列表中。然后在循环完成后,删除该列表中的所有项目:

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);
      }
    }

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);
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文