Why isn't this causing an infinite loop of events?

3.6k views Asked by At

I have a simple application that reverses any text typed to it in another textbox. The catch is, you can modify either textbox and the changes will be (literally) reflected in the other.

I wrote this code, believing for it to cause problems.

private void realText_TextChanged(object sender, EventArgs e)
{
    mirrorText.Text = mirror(realText.Text);
}

private void mirrorText_TextChanged(object sender, EventArgs e)
{
    realText.Text = mirror(mirrorText.Text);
}

private string mirror(string text)
{
    return new string(text.Reverse().ToArray()).Replace("\n\r", "\r\n");
}

I then tried it out, believing that it would cause an infinite loop (realText changes mirrorText, another event happens, mirrorText changes realText, etc). However, nothing except the intended behavior happened.

I'm of course happy about this, I could just leave it here. Or could I?

I'm quite sure the TextChanged event is supposed to be fired whenever Text is changed. Is this intended behavior of some error protection in the events, or was I just lucky? Can this code misbehave on another computer, with other build settings, etc? It can be easily fixed:

private void realText_TextChanged(object sender, EventArgs e)
{
    if (realText.Focused)
    {
        mirrorText.Text = Mirror(realText.Text);
    }
}

I'll probably do it anyway to be safe, but is it required to check this? (I'm not even going to ask if it's recommended.)

4

There are 4 answers

0
AudioBubble On BEST ANSWER

Per the comments, and as already answered, the TextChanged event is not getting raised when you set the Text property to the value it already has.

It's not clear whether this is something you can safely rely upon. It is a sensible optimisation, and I would be very surprised if future versions of .NET Framework drop it, but I cannot speak for older versions, nor for third-party implementations (Mono).

To be absolutely safe, I would not use the Focused check you put in your question. I would do exactly what the Text setter does now.

private void realText_TextChanged(object sender, EventArgs e)
{
    var newMirrorText = Mirror(realText.Text);
    if (mirrorText.Text != newMirrorText)
        mirrorText.Text = newMirrorText;
}

This has the same advantage of preventing infinite recursion, but plays more nicely with other code you may put in your form that changes the text as a result of some other event.

3
Willem van Rumpt On

The reason it doesn't cause a loop is that it checks whether the Text property actually changed, i.e. if the new value does not equal the old value. In your case the mirror function happens to reverse itself, which leads to the same text after two passes.

0
Thomas Papamihos On

If textbox has a Text, and we try to change it with the same Text, the TextChange event is not raising because new text is same as the previous. In your code, the realText_TextChanged event reverses the text and changes the mirrorText with it. The mirrorText_TextChanged event reverses the text and try to change the realText. The realText has already this text and does not raises the realText_TextChanged event.

1
Sinix On

It's pretty easy to check.

First, replace both textbox controls with

    class T : TextBox
    {
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
    }

Second, set the breakpoint on setter. Add these expressions to the Watch window:

  • Name
  • Text
  • value

Third, launch the app, copy '123' from somewhere and paste it to the first textbox. Here it goes:

1st break:

  • Name: "mirrorText"
  • Text: ""
  • value: "321"

2nd break:

  • Name: "realText"
  • Text: "123"
  • value: "123"

3rd... whoops, it does not breaks anymore. To detect why we had to go deeper. Look at referencesource: text box setter does nothing unusual, but TextBoxBase's one looks interesting:

        set {
            if (value != base.Text) { // Gotcha!
                base.Text = value;
                if (IsHandleCreated) {
                    // clear the modified flag
                    SendMessage(NativeMethods.EM_SETMODIFY, 0, 0);
                }
            }
        }

So, as hvd already answered, the reason is the textbox does not raise TextChanged if old and new values are the same. I don't think the behavior will change, at least for winforms. But if you want more robust solution, here it is:

    private void RunOnce(ref bool flag, Action callback)
    {
        if (!flag)
        {
            try
            {
                flag = true;
                callback();
            }
            finally
            {
                flag = false;
            }
        }
    }

    private bool inMirror;
    private void realText_TextChanged(object sender, EventArgs e)
    {
        RunOnce(ref inMirror, () =>
        {
            mirrorText.Text = mirror(realText.Text);
        });
    }

    private void mirrorText_TextChanged(object sender, EventArgs e)
    {
        RunOnce(ref inMirror, () =>
        {
            realText.Text = mirror(mirrorText.Text);
        });
    }

    private string mirror(string text)
    {
        return new string(text.Reverse().ToArray()).Replace("\n\r", "\r\n");
    }

P.S. mirror() will fail on surrogate pairs. Here're some solutions.