Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我有一个理解词典的问题_C#_Concurrentdictionary - Fatal编程技术网

C# 我有一个理解词典的问题

C# 我有一个理解词典的问题,c#,concurrentdictionary,C#,Concurrentdictionary,我有一个理解的问题。在安全性和性能方面,哪一个是更好的选择 private ConcurrentDictionary<string, ProfitTarget> chartTraderTP; if (chartTraderTP.ContainsKey(orderId)) { //Store values for later use in Mouse Events chartTraderTP[orderId].OrderLabelRectText = rectText

我有一个理解的问题。在安全性和性能方面,哪一个是更好的选择

private ConcurrentDictionary<string, ProfitTarget> chartTraderTP;

if (chartTraderTP.ContainsKey(orderId))
{
    //Store values for later use in Mouse Events
    chartTraderTP[orderId].OrderLabelRectText = rectTextOrderLabelTP;
    //We can draw the Rectangle based on the TextLayout used above
    if (!chartTraderTP[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);
    }
}
privateCcurrentDictionary chartTraderTP;
if(chartTraderTP.ContainsKey(订单ID))
{
//存储值以供以后在鼠标事件中使用
chartTraderTP[orderId].OrderLabelRectText=rectTextOrderLabelTP;
//我们可以根据上面使用的文本布局绘制矩形
如果(!chartTraderTP[orderId].IsMovingOrder
&&(ChartTraderDisplayStyle==ChartTraderDisplayStyle.Own
||ChartTraderDisplayStyle==ChartTraderDisplayStyle.Both)
{
RenderTarget.FillRectangle(rectTextOrderLabelTP,tpAreaBrushDx);
RenderTarget.DrawRectangle(rectTextOrderLabelTP、tpOutlineBrushDx、,
标签线宽度;
RenderTarget.DrawTextLayout(vectText、tpTextLayout、tpTextBrushDx、,
SharpDX.Direct2D1.DrawTextOptions.NoSnap);
}
}

privateCcurrentDictionary chartTraderTP;
//存储值以供以后在鼠标事件中使用
if(chartTraderTP.ContainsKey(订单ID))
{
chartTraderTP[orderId].OrderLabelRectText=rectTextOrderLabelTP;
}
//我们可以根据上面使用的文本布局绘制矩形
if(chartTraderTP.ContainsKey(orderId)&&!chartTraderTP[orderId].IsMovingOrder
&&(ChartTraderDisplayStyle==ChartTraderDisplayStyle.Own
||ChartTraderDisplayStyle==ChartTraderDisplayStyle.Both)
{
RenderTarget.FillRectangle(rectTextOrderLabelTP,tpAreaBrushDx);
RenderTarget.DrawRectangle(rectTextOrderLabelTP、tpOutlineBrushDx、,
标签线宽度;
RenderTarget.DrawTextLayout(vectText、tpTextLayout、tpTextBrushDx、,
SharpDX.Direct2D1.DrawTextOptions.NoSnap);
}

代码经常被执行,我想让访问速度尽可能快,但仍然是线程安全的。

两者都不安全,因为您正在执行多个操作,同时假设集合在操作之间没有变化,这不是一个安全的假设


由于您想要使用许多不同的操作,并且需要整个部分在逻辑上是原子的,所以只需使用一个常规的
字典
并锁定对它的访问。

字典
,一个
,再加上一个
TryGetValue
就足够了。您的意思是像下面这样的锁吗?对不起,我是新手;-)@Sidlercom您可能想使用
lock
关键字,是的。您锁定的内容以及其中包含的内容将取决于比您显示的内容更多的内容
如果找到它,则它可以与您检索的实例一起工作。@Knoop假设没有其他线程正在编辑集合中的对象,情况似乎不是这样。@Servy这是真的,但是您只需要锁定您检索的对象,而不是整本
字典。如果没有更多关于不同线程正在执行的操作的信息,很难说出任何具体的内容。@Knoop但是现在您需要使用多个不同的锁,这会大大增加复杂性。它可能是需要的,也可能不是,但是说他们可以使用
TryGetValue
并且可以完成是不太可能的。在
ConcurrentDictionary
上使用锁否定了它的所有优点,同时保留了它的缺点(笨拙的API、开销)。如果你必须
锁定
,你最好在普通的
字典
上执行。
   lock (chartTraderTP)
            {
                //Store values for later use in Mouse Events
                chartTraderTP[orderId].OrderLabelRectText = rectTextOrderLabelTP;
                //We can draw the Rectangle based on the TextLayout used above
                if (!chartTraderTP[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);
                }
            }
   lock (chartTraderTP)
            {
                //Store values for later use in Mouse Events
                chartTraderTP[orderId].OrderLabelRectText = rectTextOrderLabelTP;
                //We can draw the Rectangle based on the TextLayout used above
                if (!chartTraderTP[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);
                }
            }