in my C#-Silverlight-3-Application, I have created a class, that is doing some calculations that are used in different parts of my program. This class needs data from a database, so it calls a WCF-Service to get the data. The structure of the class looks like this:
public class ConsumptionCalculation
{
// Declare the event to notify subscribers, that the calculation has finished
public delegate void HandlerConsumtionCalculationFinished(object sender, ConsumtionCalculationArgs args);
public event HandlerConsumtionCalculationFinished CalculationFinished;
public void Do(int id)
{
// call the WCF-Service
DataContainer.instance.dataSource.GetConsumtionInfoAsync(id);
}
void dataSource_GetConsumtionInfoCompleted(object sender, GetConsumtionInfoCompletedEventArgs e)
{
// Receive the result of the WCF-Service call
// do some calculation here and put the results in the variable 'args'
// Raise an event to notify listeners, that the calculation is finished
CalculationFinished(this, args);
}
}
The objects that need the calculation-class reference it and subscribe to the CalculationFinished event.
public class IUseTheCalculation
{
ConsumptionCalculation _calcObject;
public IUseTheCalculation()
{
_calcObject = new ConsumptionCalculation();
_calcObject.CalculationFinished += new HandlerConsumptionCalculationFinished(CalculationFinishedEvent);
}
private void CalculationFinishedEvent(object sender, ConsumptionCalculationArgs args)
{
// use the calculation to display data or write it to a file or whatever
}
}
I have more than one class like the IUseTheCalculation class that each have their own ConsumptionCalculation-Object.
My problem is the following:
When I call the Do-method of the ConsumptionCalculation-object in one of the classes, ALL classes, that have a reference to any ConsumptionCalculation-object will receive the resulting CalculationFinished event. I think this is because all existing ConsuptionCalculation-objects recieve the dataSource_GetConsumtionInfoCompleted event from the WCF-Service.
But I want only the calling object to receive the event. I guess there is a standard approach to this kind of problem, but I couldn’t figure it out yet. How would you resolve this problem?
Thanks in advance,
Frank
You didn’t show where or how you declare the CalculationFinished event. If it were static, that would explain the behavior you’re seeing. But I’ll assume you already checked that. Perhaps in the debugger you could examine the objects in question to determine if they are actually diffenent instances, as you are assuming they are.
Could you use the “id” field to distinguish the various callers (and if not, perhaps add an additional parameter to do so)? This wouldn’t stop the other classes from getting notified, but they could all check quickly to see if the notification is for them, and return quickly if not.