Template for creating one event for multiple controls

I have multiple controls on a page. This happens to be a Silverlight page, but I don't think it matters to this question.

When a control value changes, a change event occurs. In this case, I do the calculation and change the value of one of the other controls. This, in turn, causes the other control to raise the changed event, which in turn causes the computation (redundantly) to do it again.

i.e. Changing the value of any control will execute the calculation and change the value of the other control — always in this case.

My initial attempt to fix this involved using a boolean flag and resetting it to its default on a second event. However, it didn't work and felt flustered.

How could you prevent the second calculation from being performed?

+1


source to share


1 answer


Boolean flag too, I guess ... Not sure why it didn't work for you. It will 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 can use the blocking operator anywhere if you are afraid of multithreading, although I don't think it is. Alternatively, you can try ... finally, to make sure InEvent is always false at the end.

+1


source







All Articles