Thread Propriedades seguras em C #
Estou tentando criar propriedades seguras de thread em C # e quero ter certeza de que estou no caminho correto - eis o que fiz -
private readonly object AvgBuyPriceLocker = new object();
private double _AvgBuyPrice;
private double AvgBuyPrice
{
get
{
lock (AvgBuyPriceLocker)
{
return _AvgBuyPrice;
}
}
set
{
lock (AvgBuyPriceLocker)
{
_AvgBuyPrice = value;
}
}
}
Lendo esta postagem, parece que essa não é a maneira correta de fazê-lo -
Segurança de threads C # com get / set
e qualquer forma, este artigo parece sugerir o contrário,
http: //www.codeproject.com/KB/cs/Synchronized.asp
lguém tem uma resposta mais definitiv
Editar
O motivo pelo qual eu quero fazer o Getter / Setter para essa propriedade é b / c. Na verdade, eu quero que ele dispare um evento quando estiver definido - para que o código seja assim -
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)
{
AvgBuyPriceUpdateListener(AvgBuyPrice);
}
else
{
throw new Exception("AvgBuyPriceUpdateListener is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sou muito novo em tornar meu encadeamento de código seguro, então fique à vontade para me dizer se estou fazendo isso da maneira totalmente errad
Va