By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

iteration on hashtable spawning new threads

P: 1
Hello i have a very annoying problem when trying to use a foreach loop on my hashtable, sometimes i get the same object twice, and i really can't figure out why, i hope someone can tell me what i am doing wrong

Hashtable tmp = Hashtable.Synchronized((Hashtable) mirrorContains[searchstring]);
foreach (DictionaryEntry entry in tmp)
{
new Thread(delegate() { commit(entry, searchstring); }).Start();
}
private void commit(DictionaryEntry entry, string searchstring)
{
MirrorInfo minfo = (MirrorInfo) entry.Key;
System.Console.WriteLine(minfo.Name);
}

problem is that the the dictionaryentry passed to the commit method sometimes refers to the same object, which shouldn't be possible on a synchronized hashtable, problem is the error is not very easy to reproduce as it only happens sometimes, i hope someone can tell me what i'm doing wrong

Regards
Martin
Apr 26 '07 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
Hello i have a very annoying problem when trying to use a foreach loop on my hashtable, sometimes i get the same object twice, and i really can't figure out why, i hope someone can tell me what i am doing wrong

Hashtable tmp = Hashtable.Synchronized((Hashtable) mirrorContains[searchstring]);
foreach (DictionaryEntry entry in tmp)
{
new Thread(delegate() { commit(entry, searchstring); }).Start();
}
private void commit(DictionaryEntry entry, string searchstring)
{
MirrorInfo minfo = (MirrorInfo) entry.Key;
System.Console.WriteLine(minfo.Name);
}

problem is that the the dictionaryentry passed to the commit method sometimes refers to the same object, which shouldn't be possible on a synchronized hashtable, problem is the error is not very easy to reproduce as it only happens sometimes, i hope someone can tell me what i'm doing wrong

Regards
Martin

Could you provide a bit more information on what the program is doing exactly?
How are threads part of it?

-Frinny
Apr 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.