This seems like a basic question, but Google has not helped me… How do I do basic thread locking in C#.NET WPF? In my fairly simple user application, I have multiple threads performing time-intensive operations that save their results into one common data structure or read from this common data structure. I’d like to be able to lock() and unlock() around access to this data structure to maintain thread safety. However, there are many different places in my code that I access this data structure from, not just one. What .NET objects are used to implement this locking?
Share
try this:
then, in your code, use:
http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.71).aspx
I guess a little more info may be helpful. IIRC, the
lock(object o)is basicly a macro that will be expanded by the compiler and then restrict access to your safe code, which is what you’d expect. What isn’t necessarily obvious is that any objectocan be a lock, and eachois needed for each safe lock. if I haveand perform a
lock(a)then that will not prevent access to something with alock(b)around it. Most code samples show alock(this)which will lock to the current instance of the particular class, which is not what you want if you need a global lock and have multiple instances of that class running around.