如何解决ConcurrentDictionary 和 Lazy<T>
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; } }
}
我想用以下内容从上面更改字典:
private ConcurrentDictionary<string,Lazy<ProfitTarget>> dictTP = new ConcurrentDictionary<string,Lazy<ProfitTarget>>();
“RectangleF rectTextOrderLabelTP”将通过另一个类“ProfitTarget”中的属性更新,就像现在一样(AddOrUpdate)。不幸的是,我无法将传统字典转换为新的 Lazy to work,谁能帮帮我?
我想确保一切都是线程安全的,并且没有使用“锁”,这就是为什么我遇到了在网络上使用 Lazy 的可能性。 使用到目前为止的代码,有时会收到错误消息,例如:“System.NullReferenceException”或“KeyNotFoundException”。
不幸的是,我是编程新手,总是喜欢学习新东西。我非常感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。