public class SystemBase
{
***private ConcurrentDictionary<string, ProfitTarget> dictTP = new ConcurrentDictionary<string, ProfitTarget>();***
private void DrawOrderLineTP(Order order, ChartControl chartControl, ChartScale chartScale, string orderId, double openProfit, double tickValue)
{
RectangleF rectTextOrderLabelTP = new RectangleF(vectText.X - 2, vectText.Y, tpTextLayout.Metrics.Width + 4, tpTextLayout.Metrics.Height);
***var newValueText = new ProfitTarget { OrderLabelRectText = rectTextOrderLabelTP };***
if (dictTP != null && newValueText != null)
{
dictTP.AddOrUpdate(orderId, newValueText, (key, oldValueText) =>
{
if (newValueText.OrderLabelRectText != oldValueText.OrderLabelRectText)
oldValueText.OrderLabelRectText = newValueText.OrderLabelRectText;
//We can draw the Rectangle based on the TextLayout used above
if (!dictTP[orderId].IsMovingOrder && (ChartTraderDisplayStyle == ChartTraderDisplayStyle.Own || ChartTraderDisplayStyle == ChartTraderDisplayStyle.Both))
{
RenderTarget.FillRectangle(rectTextOrderLabelTP, tpAreaBrushDx);
RenderTarget.DrawRectangle(rectTextOrderLabelTP, tpOutlineBrushDx, LabelOutlineWidthTP);
RenderTarget.DrawTextLayout(vectText, tpTextLayout, tpTextBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
}
//return oldValueText, since it should be updated, instead of being replaced
return oldValueText;
});
}
}
}
public class ProfitTarget : IEquatable<ProfitTarget>
{
private RectangleF orderLabelRectText;
[CLSCompliant(false)]
public RectangleF OrderLabelRectText { get { return orderLabelRectText; } set { orderLabelRectText = value; } }
}
i would like to change the dictionary from above with the following:
private ConcurrentDictionary<string, Lazy<ProfitTarget>> dictTP = new ConcurrentDictionary<string, Lazy<ProfitTarget>>();
the "RectangleF rectTextOrderLabelTP " is to be brought up to date via the property in the other class "ProfitTarget", as it is now (AddOrUpdate). Unfortunately, I can't get the conversion from the conventional dictionary to the new Lazy to work, can anyone help me?
I want to make sure that everything is thread safe and that no "lock" is used, which is why I came across the possibility of using the Lazy on the web. with the code i have so far, sometimes get error messages like: "System.NullReferenceException" or "KeyNotFoundException".
Unfortunately, I'm a newbie in programming and always like to learn something new. I'm very grateful for any help!