KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to create thread safe properties in C# and I want to make sure that I am on the correct path - here is what I have done - private readonly object AvgBuyPriceLocker = new object(); private double _AvgBuyPrice; private double AvgBuyPrice { get { lock (AvgBuyPriceLocker) { return _AvgBuyPrice; } } set { lock (AvgBuyPriceLocker) { _AvgBuyPrice = value; } } } Reading this posting, it would seem as if this isn't the correct way of doing it - C# thread safety with get/set however, this article seems to suggest otherwise, http://www.codeproject.com/KB/cs/Synchronized.aspx Does anybody have a more definitive answer? Edit: The reason that I want to do the Getter/Setter for this property is b/c I actually want it to fire an event when it is set - so the code would actually be like this - public class PLTracker { public PLEvents Events; private readonly object AvgBuyPriceLocker = new object(); private double _AvgBuyPrice; private double AvgBuyPrice { get { lock (AvgBuyPriceLocker) { return _AvgBuyPrice; } } set { lock (AvgBuyPriceLocker) { Events.AvgBuyPriceUpdate(value); _AvgBuyPrice = value; } } } } public class PLEvents { public delegate void PLUpdateHandler(double Update); public event PLUpdateHandler AvgBuyPriceUpdateListener; public void AvgBuyPriceUpdate(double AvgBuyPrice) { lock (this) { try { if (AvgBuyPriceUpdateListener!= null)
Tags (comma-separated)
Save Edits
Cancel