Solved

Dictionary and a struct

Posted on 2013-01-16
4
481 Views
Last Modified: 2013-01-16
hi -- I'm using C# and VS2010 with .net 4.
i have a dictionary that i use to keep state information in my program. the value part of the key value pair is a struct (called "node"). it seems that i can only update "node" as a single entity rather than updating individual elements when i have fresh data. i have the additional problem of the data coming from two different threads. i set up the dictionary as static and use flags to protect the write operation. but every now and then (every couple hours) i get an enumeration error, which i suspect is related to the update process.
is there a better approach?
thanks,
0
Comment
Question by:jdcoburn
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 167 total points
ID: 38783859
Why don't you use the ConcurrentDictionary class?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 167 total points
ID: 38784128
"it seems that i can only update "node" as a single entity rather than updating individual elements when i have fresh data."

Switch from a struct to a class.
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 166 total points
ID: 38784277
About structs, many people make the same mistake by forgetting that structures are value type. See more details here, e.g.:

http://stackoverflow.com/questions/7126318/how-do-you-assign-values-to-structure-elements-in-a-list-in-vb-net


Use class instead.

ConcurrentDictionary should be a good solution but I have a feeling that something else happens.
0
 

Author Closing Comment

by:jdcoburn
ID: 38785087
thanks. all the solutions added value in different ways.<br />Jim
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now