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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...