views:

351

answers:

1

I have several controls on a page. It happens to be a Silverlight page but I don't think that it matters for this question.

When a control's value changes, a change event is raised. In this event I do a calculation and change the value of one of the other controls. This in turn causes the other control to raise a changed event which in turn causes the calculation to be (redundantly) done again.

i.e. The changing of a value of any control will cause a calculation to be performed and the value of another control to change - always in this use case.

My initial attempt at solving this involved using a boolean flag and resetting it to the default value in the second event. However, this did not work and felt hokey.

How would you go about preventing the second calculation from being done?

+1  A: 

The boolean flag too, I think... Don't know why it didn't work for you. It'd be something like this:

private bool InEvent = false;
public void MyEventHandler(object sender, EventArgs e)
{
    if ( InEvent )
        return;
    InEvent = true;
    // Do stuff here
    InEvent = false;
}

You might use a lock statement somewhere if you're afraid of multi-threading, though I don't think this is the case. Also, you might want to do a try...finally to make sure that the InEvent is always set to false in the end.

Vilx-
This is similar to what I'm talking about except that there are 2 events so it would be MyEventHanderA() and MyEventHandlerB(). Each call a common calculation function.
Guy