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);
}
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.
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.
Any ideas?
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.
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.
Update3
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?
Update 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.