This question already has answers here:
What's the best way of implementing a thread-safe Dictionary?
(8 answers)
Closed 5 years ago.
whats is the easiest way to make C# dictionary access thread safe? Preferably just using lock(object) but any other ideas welcome!
In .NET 4 you have the ConcurrentDictionary class.
If you need to use an older version of .NET, and want to write it yourself:
wrap a Dictionary as a private field in your class
use a separate private object lockObject
take a lock on that lockObject around every access to the dictionary
Related
This question already has answers here:
is locking necessary for Dictionary lookup?
(9 answers)
Closed 5 years ago.
I'm using .netcore with C#, I have a System.Collections.Generic.Dictionary<> that is shared and been access by multiple tasks, but there is only one task that is writing to and updating the key value pairs, and all other tasks(more than one) are only reading from the dictionary. In this situation, do I need to use a lock to protect it, in terms of thread safety?
Yes. Dictionary cannot be safely read from while a write is in process; the internal data storage is not in a valid state, and so exceptions or nonsensical results can arise.
Use a ConcurrentDictionary instead.
This question already has answers here:
When should the volatile keyword be used in C#?
(11 answers)
Closed 7 years ago.
Is there any case in which volatile is useful in the context of single-threaded programming? I know it's used to make sure the value of the variable is always actually checked in memory so is there any case in which that value may change (in a ST app) in a way that the app/compiler won't notice?
No, it's not necessary. It is used to synchronize the memory content between the threads which in case you have only one it doesn't make sense.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immutable List in C#
Is it possible to make a list immutable
You can use ReadOnlyCollection<T> instead.
List<T>.AsReadOnly() returns a readonly wrapper, so that you can't add/remove/replace elements.
To be truly immutable, the type T must be an immutable Type.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Which collection for storing unique strings?
I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.
Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?
.NET 3.5 includes the HashSet<T> collection type, which sounds like what you want.
HashSet<T>