I have this problem - I have a hashtable, containing a list of filenames.
Every 60 seconds, I have a thread that enumerates thru this hashtable, and
based on some simple logic, some of the items in the hashtable has to be
removed.
But when I remove a pair from the hashtable, inside the enumeration loop, I
get an exception.
Why cant I remove from a hashtable, while enumerating ?
My code looks something like this :
for each r in camImages
if r.value = x then
camImages.remov e(r.key)
end if
next
When the above code is running, the first time the "camImages.remo ve(r.key)"
line is run, I get an exception in the last line, containing "next".
Can someone explain to me what I am doing wrong ?
-
Regards,
Tony Fonager
Netcoders ApS 2 4577
Tony, Why cant I remove from a hashtable, while enumerating ?
Because the enumerator (an object that implements IEnumerator, an interface
used by For Each) may 'loose' track of where it is if you delete an item,
the element the enumerator is on is no longer in the collection, so when it
goes to get the next element it has no idea where to look, as the internal
structure of the HashTable has changed. Yea, Yea, The HashTable & enumerator
could let each other know when this happens, but the 'expense' of doing this
is greater then the 'benefit'.
Generally the way to delete items while enumerating is to make a copy of the
Keys collection, then enumerator of this copy, or to add the items you want
deleted to a second collection (ArrayList for example). then delete based on
the items in this second collection. I tend to go for the second.
To make a copy of the Keys collection see Hashtable.Keys. CopyTo.
Hope this helps
Jay
"Tony" <to**@nospam-fonager.dk> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. I have this problem - I have a hashtable, containing a list of filenames.
Every 60 seconds, I have a thread that enumerates thru this hashtable, and based on some simple logic, some of the items in the hashtable has to be removed.
But when I remove a pair from the hashtable, inside the enumeration loop,
I get an exception.
Why cant I remove from a hashtable, while enumerating ?
My code looks something like this :
for each r in camImages if r.value = x then camImages.remov e(r.key) end if next
When the above code is running, the first time the
"camImages.remo ve(r.key)" line is run, I get an exception in the last line, containing "next".
Can someone explain to me what I am doing wrong ?
- Regards, Tony Fonager
Netcoders ApS
Jay,
Thanks for your reply and suggestions - I thought of doing it the same way
as you suggest, with a copy of the keys.
But before that, I just had to know if I was doing something wrong ;-)
-
Regards,
Tony Fonager
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:#Z******** ******@TK2MSFTN GP10.phx.gbl... Tony, Why cant I remove from a hashtable, while enumerating ? Because the enumerator (an object that implements IEnumerator, an
interface used by For Each) may 'loose' track of where it is if you delete an item, the element the enumerator is on is no longer in the collection, so when
it goes to get the next element it has no idea where to look, as the internal structure of the HashTable has changed. Yea, Yea, The HashTable &
enumerator could let each other know when this happens, but the 'expense' of doing
this is greater then the 'benefit'.
Generally the way to delete items while enumerating is to make a copy of
the Keys collection, then enumerator of this copy, or to add the items you
want deleted to a second collection (ArrayList for example). then delete based
on the items in this second collection. I tend to go for the second.
To make a copy of the Keys collection see Hashtable.Keys. CopyTo.
Hope this helps Jay This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Keith Langer |
last post by:
I have a hashtable which is accessed by two threads. One thread does
all writing and enumeration in the hashtable, and the other thread has
read-only access to the table directly through keys but never with
enumerators. Is there any reason to worry about synclocks?
thanks,
Keith
|
by: Kevin |
last post by:
Hi,
How can I enumerate a linked list while being hable to delete any number of
elements while enumerating ?
I was using:
struct st {
...
struct st *prev;
|
by: MFRASER |
last post by:
How do I go about looping through a hash table and removing items. I know
how do this in a collectionbase, but can't iterate through the hash table
with out getting an error.
Here is my sample code for a collection base
for(int i = this.Values.Count; i > 0 ; i--)
{
//Set local object
|
by: Ken |
last post by:
I have a C# Program where multiple threads will operate on a same Hashtable.
This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable)
method, so no further Lock statements are used before adding, removing or
iterating the Hashtable. The program runs in a high workload environment.
After running a few days, now it suddenly catchs this Exception when
inserting a pair of key and object,
stacktrace =...
|
by: 小葉南洋杉 |
last post by:
Hello,
I'm using System.Security.Cryptography to develop some XML APs.
However, the RSA class in my program is as msdn example shows:
RSA* key = RSA::Create();
signedXml->SigningKey = key;
This method works, but the keypair varies each run.
How can I load a RSA keypair into the RSA class?
TIA
| |
by: Ryan Liu |
last post by:
Hashtable.GetEnumerator is not thread safe, but what about I write I loop go
though Hashtable.Values, is that thread saft?
Thanks a lot!
|
by: msnews.microsoft.com |
last post by:
So what is the best way to iterate through the hash table to remove all the
members?
i tried using the enumerator but it gets an exception as it removes each
element.
dim de as dictionaryEntry
for each de in xHashTable
xHashTable.remove(de.key)
next
|
by: VISHAL |
last post by:
Hello,
I am working with VC++ 2003.I am having a problem HashTables.How do i get a
value for a specifc key without enumerating through all the key value pair in
the HashTable.
Any help will be appreciated.
Thanks
|
by: Mark S. |
last post by:
Much to my surprised the code below compiled and ran. I just don't know
enough about threading to know for sure if this is too good to be true.
I'm attempting to isolate the Hashtable lock to "row level", the whys aren't
important at this time. Does this code actually do what I want?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |