重复键错误,但在使用调试点运行时不会重新创建

ApsSanj

我正在实现向树视图添加一些数据的功能-MVP(应用程序由Word功能区(VSTO)+ C#代码库组成)。下面是我的代码。

private ElementTreeNode LoadElement(Element element, ElementTreeNode parent = null)
{
    ElementTreeNode loadElement;
    ElementTreeNode node;

    //Add a node to the TreeView that represents the element.
    loadElement = Add(text: GetElementNodeText(element.Name), parent: parent);
    loadElement.Element = element;

    //Add a node to the TreeView for conditions
    if (element.TSS.Conditions.Count > 0)
    {
        Add(text: "Conditions", parent: loadElement).Conditions = element.TSS.Conditions;
    }

    //Add a node to the TreeView control for each image within the element.
    foreach (Image image in element.FrameSets.Active.Images)
    {
        Add(text: GetImageNodeText, parent: loadElement).Image = image;
    }

    //Call this procedure recursively to add any nested elements.
    foreach (Element objElement in element.FrameSets.Active.Elements)
    {
        LoadElement(element: objElement, parent: loadElement);
    }

    return loadElement;
}

添加方法:

private ElementTreeNode Add(string text, string key = "", ElementTreeNode parent = null)
{
    try
    {
        TreeNode node = null;
        //Create a random key if a key is not specified.
        if (key.Length == 0)
        {
            key = "ElementTreeNode_" + GetRandomNumber();
        }

        //Add a node to the TreeView control.
        if (parent == null) //..............point 1
        {
            node = treeView.Nodes.Add(key: key, text: text);
        }
        else // ..............point 2
        {
            node = parent.Node.Nodes.Add(key: key, text: text);
        }
        //Add a new ElementTreeNode object to this collection. It will have the same key as objNode.
        ElementTreeNode objNode = new ElementTreeNode(node);                
        elementTreeNodesCollection.Add(key: key, value: objNode);

        return objNode;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine + "ElementTreeNodes.Add","MyApp,
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
    }
}

创建随机数:

public long GetRandomNumber()
{
    Random rnd = new Random();
    return rnd.Next();
}

问题是,在我的测试运行中,此代码首先将父元素添加到树视图中,然后添加到集合(Dictionary<string, ElementTreeNode> elementTreeNodesCollection)中。在下一步中,“条件”树节点将作为父节点下的子节点添加到树节点。当我尝试将其添加到时elementTreeNodesCollection,出现以下错误。

具有相同密钥的项目已被添加

最大的问题是,如果我在Add方法中将调试点放在第1点或之前(整个代码库中没有其他调试点),则不会收到任何错误,并且已将项目正确添加到树视图和集合中。

但是,如果我在第2点或之后添加调试点,则会收到错误消息。另外,当没有断点时,我也会收到此错误。

我尝试将密钥设置为递增数字,然后代码运行正常。但是我需要在这里使用随机数,因为LoadElement方法涉及递归。这个问题与随机数生成功能有关吗?还是其他情况?我真的被困在这里。

有人知道我在这里做错了什么吗?

先感谢您。

乔恩·斯基特

我强烈怀疑问题出在您的GetRandomNumber()方法上,具体取决于当前时间。如果您连续多次调用它,最终将得到相同的号码。

如果您在两次通话之间稍等片刻(例如,由于在断点处暂停),您将获得不同的随机数。

我建议最简单的选择是使数字根本不是随机的-保留一个计数器,该计数器在每次需要创建新密钥时都会增加。或者您可以使用Guid.NewGuid()并将其转换为字符串或类似名称。

如果您确实想使用Random,请创建一个Random用于所有调用的实例,但是要注意这Random不是线程安全的;如果您需要跨多个线程进行工作,那么一切都会变得更加复杂。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Kafka在运行时重新创建Kafka流拓扑

每次运行时更改都会重新创建MediaBrowserServiceCompat实例

在Java仍在运行时重新创建日志文件

cout 新创建的对象抛出运行时错误

在运行时更改语言环境时刷新(重新创建)后退堆栈中的活动

Spring 应用程序尝试在每次运行时重新创建数据库而不是更新

尝试从新创建的工作簿中删除工作表时出现运行时错误 - Outlook VBA

Blender 使用 python 脚本创建屏幕截图,但在后台运行时不会

如何在Android M或更高版本中在运行时更改权限时防止重新创建活动

如何在运行时每0.2秒在屏幕上的不同位置重新创建屏幕上的相同节点?

如何调试要求我玩游戏才能重新创建错误的视频游戏?

使用 Pygame 重新创建 PacMan

在调试时在pool.close()上引发异常,但在运行时则不会

Virtualenv 不会创建环境;运行时错误

即使使用START_STICKY,Android也不会重新创建服务

在React中使用键重新创建组件是一种不好的做法吗?

tee -a不会重新创建文件

角度URL更改不会导致重新创建组件

片段重新创建后,ViewModel不会保留数据

如何通过键重新创建表?

文本到列,运行时带有逗号循环,但在调试时未生成错误

调试模糊的 iOS 运行时错误?

如何调试Pascal运行时错误?

如何调试此运行时错误?

调试Python 3运行时错误

是否可以使用原始堆栈和堆栈帧重新创建错误对象?

我正在尝试使用加法重新创建 Diffie-Hellman 密钥交换,但出现错误

当我运行程序时出现运行时错误,但在使用调试器时却没有

仅使用CSS重新创建偏斜效果