异步循环中为空引用,但对象没有空值 [英] Null reference in async loop, but object has no nulls

查看:86
本文介绍了异步循环中为空引用,但对象没有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,无法解决此问题帖子,因为它描述了简单/基本的对象引用问题.我遇到的问题与多线程异步处理有关,另一篇文章并没有解决.

First, this is not answered by this SO post, as that describes simple/basic object reference problems. What I'm experiencing is related to multithreaded async processing, which the other post doesn't solve.

我有一个多线程.NET winforms应用程序,我正在这样做:

I have a multithreaded .NET winforms app and I'm doing this:

if ( paramList != null ) {
   lock ( paramList ) {
      foreach ( DictionaryEntry param in paramList ) {
         command.Parameters.AddWithValue(param.Key.ToString(), param.Value);
      }
   }
}

paramListOrderedDictionary.

我偶尔在foreach行上收到此错误:

I sporadically get this error on the foreach line:

对象引用未设置为对象的实例.

Object reference not set to an instance of an object.

如您所见,param.Key为空,param.Value为空.但这没有任何意义,因为paramList中没有空值,如您在此处看到的:

As you can see, param.Key is null and param.Value is null. But this makes no sense because there are no nulls in paramList, as you can see here:

在屏幕快照中,您只能看到索引2,但是我也检查了索引0和1,同样的东西,有效数据,没有null.

In the screenshot you can see only index 2, but I examined index 0 and 1 also, same thing, valid data, no nulls.

我对多线程应用程序没有经验,但是由于

I'm not experienced with multithreaded apps, but I put that block in a lock() due to responses in this SO post. Before putting in the lock() I was sporadically getting the error Collection was modified; enumeration operation may not execute. After putting in the lock, that error went away, but now I'm getting the object reference as shown above.

该怎么办?

编辑

根据一些海报的建议,我这样做了:

Taking the advice of a few posters, I did this:

private static object syncLock = new object();

然后降低用法:

lock ( syncLock ) {
   if ( paramList != null ) {
       foreach ( DictionaryEntry param in paramList ) {
          command.Parameters.AddWithValue(param.Key.ToString(), param.Value);
       }
   }
}

这似乎已经解决了对象引用错误(感谢大家),但是现在我偶尔会得到:

That seems to have solved the object reference error (thanks everyone), but now I sporadically get:

集合已修改;枚举操作可能无法执行.

Collection was modified; enumeration operation may not execute.

由于尝试了这种新方法后出现了完全不同的错误,因此我创建了一个新的这样的问题.我不确定这样做是否正确,因为现在看来这些问题是相关的,而我只是看到同一核心问题的不同症状.

Because I got a completely different error after trying this new approach, I created a new SO question. I'm not sure if that was the right thing to do because now it seems these problems are related and I'm just seeing different symptoms of the same core problem.

如果有人有想法,仍在寻找解决方案.

Still looking for a solution if anyone has ideas.

推荐答案

(首选)更改您的paramList,因此您无需锁定它.

(Preferred) Change your paramList so you don't need to lock it.

如果您需要共享,则:

  1. 锁定一个不变的对象,例如private readonly object l = new object();private static readonly(如果需要).
  2. 检查是否填充了paramList的代码.
  1. Lock on a object that doesn't change e.g. private readonly object l = new object();, or private static readonly, if needed.
  2. Check that the code that fills paramList is also locked.

这篇关于异步循环中为空引用,但对象没有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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