发生System.ArgumentException [英] System.ArgumentException occurred

查看:242
本文介绍了发生System.ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的行为很奇怪.这是代码:

I'm getting a strange behavior. This is the code:

...
private Object lockobj = new Object();
private Dictionary<String, BasicTagBean> toVerifyTags = null;

public void verifyTags(List<BasicTagBean> tags)
{
    System.Diagnostics.Debug.WriteLine("Thread ID: " + Thread.CurrentThread.ManagedThreadId);
    lock (lockobj)
    {
        foreach (BasicTagBean tag in tags)
        {
            if (!alreadyVerified.ContainsKey(tag.EPC))
            {
                toVerifyTags.Add(tag.EPC, tag);
            }
        }
    }
...

有时候我遇到了例外情况

Sometimes I got this exception

'System.ArgumentException' occurred in mscorlib.dll

此行代码:

toVerifyTags.Add(tag.EPC, tag);

该异常指的是将一个已经存在的元素错误地添加到集合中,但是我对此进行了检查.可能是线程问题,但应用程序输出始终显示相同的线程ID.我正在使用C#Pocketpc版本3.5.

the exception refer to wrong add of an already existing element into collection, but I check this. Maybe a thread problem but application output shows always the same thread id. I'm using c# pocketpc version 3.5.

推荐答案

该异常似乎告诉您您要添加到toVerifyTags中的密钥已经存在.您并没有检查密钥是否已在正确的字典中存在.

The exception seems to tell you that the key you are trying to add in toVerifyTags already exists. You weren't checking if the key already existed in the right dictionary.

public void verifyTags(List<BasicTagBean> tags)
{
    System.Diagnostics.Debug.WriteLine("Thread ID: " + Thread.CurrentThread.ManagedThreadId);
    lock (lockobj)
    {
        foreach (BasicTagBean tag in tags)
        {
            if (!toVerifyTags.ContainsKey(tag.EPC))
            {
                toVerifyTags.Add(tag.EPC, tag);
            }
        }
    }

这篇关于发生System.ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆