在调用开始之前的迭代中值被更改 [英] Value getting changed in the iteration before the call begins

查看:29
本文介绍了在调用开始之前的迭代中值被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有以下代码.

I've the following code in my app.

MyEventHandler handler = null; //Declare the handler

foreach (string pname in group)
{
  handler = getHandler(pname); //Get the handler
  if(handler == null)
  {                        
      throw new KeyNotFoundException("No user " + pname + " could be found");
  }
  //invoke the handler
  handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
}

所以我得到处理程序并调用 BeginInvoke 方法.但是在 BeginInvoke 被调用之前,它会进入下一次迭代并且处理程序值被更改.所以 BeginInvoke 正在参与这个新的处理程序.

So i get the handler and call BeginInvoke method. But before BeginInvoke gets called it goes to next iteration and the handler value gets changed. So the BeginInvoke is getting involved for this new handler.

希望你明白我的意思.那么我怎样才能消除这个问题呢?我不想在 BeginInvoke 之后调用 sleep,因为我觉得这是浪费时间.

Hope you get my point. So how can i eliminate this issue? I dont want to call sleep after BeginInvoke as i feel it is a loss of time.

有什么想法吗?

更新 1我很确定在调用 BeginInvoke() 之前处理程序对象已更改.我猜BeginInvoke需要一些时间来创建一个单独的线程来调用另一个函数.

Update1 I'm pretty sure that the handler object gets changed before BeginInvoke() is called. I guess that the BeginInvoke takes some time to create a separate thread to call the other function.

更新 2此代码位于 WCF 服务中,客户端调用一个函数,该函数又使用此函数.我在我的服务器中为每个客户端存储了单独的处理程序.WCF 服务具有双工合同,其中包含客户端的单独会话.我看到在执行此函数后,同一用户被调用了两次.但是我设置了一个断点并调试它(这给了 BeginInvoke 调用函数的必要时间)它完美"地工作.我非常确定我在线程中也遇到了这个问题,我在一个循环中创建了多个线程.如果线程委托具有参数 a、b、c,并且在下一次迭代开始时更改它,则会发生相同的行为.我不知道你们中有多少人以前遇到过这个问题.如果我放了一个 Sleep() 或者如果我制作处理程序的副本并使用 copy 调用它,它将起作用.

Update2 This code is in a WCF service and the clients call a function which in turn makes use of this function. I've separate handlers stored in my server for each client. The WCF service has a duplex contract with separates sessions for the client. I see that after this function is executed same user is getting invoked twice. But i put a break point and debug it (which gives the BeginInvoke the necessary time to call the function) it works "PERFECTLY". I very sure i faced this problem in threading too where i create multiple threads in a loop. If the thread delegate has parameters a,b,c and if you change it at the beginning of the next iteration the same behavior occurs. I dono how many of you people have experienced this issue before. If i put a Sleep() or if i make a copy of the handler and invoke it using copy it'll work.

更新 3

好的,我现在已经测试过了.我刚刚添加了 Thread.Sleep() 如下.

Okie, i've tested it now. I just added the Thread.Sleep() as follows.

chatTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
Thread.Sleep(500);

它就像一个魅力.有什么想法吗?

and it is working like a charm. Any thoughts?

更新 4

我创建了一个线程示例来演示该问题和 我已经在这里上传了.我希望对此的解决方案也能解决我的问题.请检查样品.

I've created a thread sample demonstrating the issue and i've uploaded it here. I hope a solution to this will resolve my issue too. Kindly check the sample.

推荐答案

我不明白为什么会发生这种情况 - 您发布的代码不可能重现您描述的行为.完全合理的是,BeginInvoke 调用可能不会立即实际执行任何操作,并且下一次迭代可能会在您实际看到该调用执行任何操作之前发生 - 因为它将排队等待由工作线程处理.

I can't see why this would happen - the code you posted cannot possibly reproduce the behaviour you describe. It's entirely reasonable that the BeginInvoke call might not actual do anything straight away, and that the next iteration might occur before you actually see that call do anything - since it will be queued for processing by a worker thread.

这并不意味着正在调用不同的处理程序 - 一旦调用 BeginInvoke 就会捕获要调用的处理程序,因此之后局部变量是否发生更改无关紧要.

还有——你为什么把锁放在这儿?除非多个线程同时对同一个可枚举进行此处理(在这种情况下,您为什么要这样做),否则我看不出您锁定的任何原因.

Also - why have you got the lock here? Unless multiple threads are doing this processing at the same time over the same enumerable (in which case why would you do that) I can't see any reason why you would lock.

我还要说的是,如果您通过在调试器中看到的内容来判断这种行为,那么您不必担心 - 通过这样做,您将从调试器中获得有趣"的结果,并且使用多个线程在混合中,重要的是在线程"调试器窗口中切换线程.

I would also say that if you're judging this behaviour by what you see in the debugger, then you shouldn't worry - you'll get 'interesting' results from the debugger by doing this, and with the multiple threads in the mix it's important to switch threads in the 'Threads' debugger window.

问题是 - 您的程序是否真的按照您的预期运行?如果是这样,但您在调试时看到了这种奇怪的行为 - 那么这是完全正常的.

The question is - does your program actually do what you expect? If so, but you're seeing this strange behaviour whilst debugging - then that's entirely normal.

正如一些评论所说 - 您发布的代码不能完全是产生问题的原因.例如,如果handler"是在执行此迭代的多个线程之间共享的局部变量,那么,是的,您可能会得到这样的结果.但是一个方法的局部变量只能被当前在该方法中的同一个线程修改(和读取);该规则的唯一例外是如果 handler 引用随后作为 ref 传递给另一个线程方法.

As a few comments have stated - the code you posted can't be exactly what's producing the problem. If, for example, 'handler' is a local variable shared between multiple threads that then perform this iteration then, yes, you could get something like this. But a variable local to a method can only be modified (and indeed read) by the same thread that's currently in that method; the only exception to that rule being if the handler reference is then passed out to another threaded method as a ref.

这篇关于在调用开始之前的迭代中值被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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