tags:

views:

370

answers:

2

Issue:

Our application crashes when a user type some text into the WPF TextBox. Please help! The easy 3 minutes reproduction appears below

Reproduction:

  1. Register a WPF TextBox (m_textBox) TextChanged event to the method below

    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        // This line of code throws Unhandled exception when typing '^' char in 
        // the text box (in United "States-International" keyboard)
        m_textBox.Dispatcher.Invoke(DispatcherPriority.Normal, new SendOrPostCallback(Foo), null);
    }
    
    
    private void Foo(object state)
    {
        //Do nothing
    }
    
  2. Change your keyboard to "United States - International"

  3. Run the application and type '^' twice in the TextBox

Aplication will crash!!!!

A: 

I could reproduce this error. Typing ^q or ~~ will also cause the crash. The exception that is thrown states that composition has already been completed. I managed to work around it by using the async BeginInvoke instead of Invoke. Will that work for you?

m_textBox.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SendOrPostCallback(Foo), null);
Robert Jeppesen
Unfortunately we must have a synchroneous execution in that context. BeginInvoke is not good enough.This is such a simple program and I wonder if I'm the first one to encounter it. There is no documentation regards the issue of the exception "'Composition' has already..."
Nir
+1  A: 

The problem is the composition code doesn't expect to get additional input events until your TextChanged handler returns. When you call Dispatcher.Invoke, it causes the Windows message queue to be processed causing additional input events.

In my opinion this is a bug because the composition engine should be robust against re-entrancy when firing user-specified events.

Ray Burns
This code crashes only for certain rare charcters under certain regional configuration. I've already reported this issue in Connect: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=481338
Nir