Domanda

I am using a WPF TextBox to perform server side spell checking in a web application. I know it's a naughty thing to add the PresentationFramework to a web app hosted in IIS, but I did it anyway. Now I'm paying the price.

I'm doing something like this:

var spellCheckTextBox = new TextBox {
   Text = text
};

int errorIndex = spellCheckTextBox.GetNextSpellingErrorCharacterIndex(startPosition, LogicalDirection.Forward);

To do this I have to call the method in an STA as this article outlines.

I am getting an Win32Exception: The operation completed successfully exception, I think this is because I have exceeded the maximum number of handles that a process can allocate. The method that I am allocating the text box in finishes successfully but I have a feeling that the underlying Win32 graphics context is not being cleaned up. The TextBox does not implement IDisposable. How can I make sure it cleans up any resources it has used?

I'm thinking that if it was on a xaml page when the page went down the internals of the Presentation Framework might do that clean up but where it is it is not happening.

If anyone is interested here is the stack trace:

[Win32Exception: The operation completed successfully]
    MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d):15
    MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks):479
    System.Windows.Threading.Dispatcher..ctor():136
    System.Windows.Threading.Dispatcher.get_CurrentDispatcher():14
    System.Windows.DependencyObject..ctor():0
    System.Windows.Media.Visual..ctor(ResourceType resourceType):11
    System.Windows.UIElement..ctor():0
    System.Windows.FrameworkElement..ctor():11
    System.Windows.Controls.Control..ctor():0
    System.Windows.Controls.Primitives.TextBoxBase..ctor():0
    System.Windows.Controls.TextBox..ctor():11

I have found some other people with similar issues here and here I'm theorising it may be related to this (although this is old). It seems to be that in most cases the Win32 graphics stuff is leaking or not being cleaned up.

È stato utile?

Soluzione

Thanks to HighCores comments I am now creating the text box on a dispatcher thread and everything is working fine. For those who are interested there is a repository here which uses the WPF text box to do spell checks in this fashion.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top