473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deleting keypair from hashtable while enumerating !??!

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
Nov 19 '05 #1
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

Nov 19 '05 #2
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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1800
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
1
1590
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;
2
24640
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
33
3326
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 =...
0
948
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
1
1647
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!
3
1493
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
1
1280
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
4
1673
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;
0
9715
marktang
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...
0
9595
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,...
1
10356
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,...
0
9176
agi2029
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...
0
5536
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...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.