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.remove(r.key)
end if
next
When the above code is running, the first time the "camImages.remove(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 4542
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****************@TK2MSFTNGP09.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.remove(r.key) end if next
When the above code is running, the first time the
"camImages.remove(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********@email.msn.com> wrote in message
news:#Z**************@TK2MSFTNGP10.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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |