WPF AutomationPeer.Initialize periodically throws NullReferenceException

I have a WCF service that starts a WPF application to take screenshots.

In 95% of cases, everything works fine. But sometimes I get a TypeInitializeException when I call the myWindow.Show method .

An internal exception ( NullReferenceException ) occurred in the static Initialize method of the AutomationPeer class (PresentationCore.dll).

Full stack trace:

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationCore.dll

More information: An initializer of type "System.Windows.Automation.Peers.AutomationPeer" threw an exception.
at System.Windows.Automation.Peers.AutomationPeer.RaiseFocusChangedEventHelper (IInputElement newFocus) at System.Windows.Input.KeyboardDevice.ChangeFocus (DependencyObject focus, Int32 timestamp) at System.Windows.Input.FeyboardDevice , Boolean askNew, Boolean forceToNullIfFailed) at System.Windows.Input.KeyboardDevice.Focus (IInputElement element) at System.Windows.Interop.HwndKeyboardInputProvider.OnSetFocus (IntPtr hwnd) at System.Windows.Interop.Hwndage message WindowMessage, IntPtr wParam, IntPtr lParam, Boolean & handled) in System.Windows.Interop.HwndSource.InputFilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & Intra.H. , Int32 msg, IntPtr wParam, IntPtr lParam,Boolean & handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation (Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper (Object delegate method, object arguments, int32 numArgs, catchHandler delegate) in System.Windows.Threading.Dispatcher.LegacyInvokeImpl (DispatcherPriority priority, TimeSpan timeout, delegate method, object arguments, Int32 numArgs) in MS.Win32.HwndSubclass.SubclassWinter hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)TryCatchWhen (Source object, delegate method, object arguments, int32 numArgs, catchHandler delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl (DispatcherPriority priority, TimeSpan timeout, delegate method, object arguments, Int32 numArgs) at MS.Win32. HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)TryCatchWhen (Source object, delegate method, object arguments, int32 numArgs, catchHandler delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl (DispatcherPriority priority, TimeSpan timeout, delegate method, object arguments, Int32 numArgs) at MS.Win32. HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

================= Internal Exception: [NullReferenceException] at System.Windows.Automation.Peers.AutomationPeer.Initialize () at System.Windows.Automation.Peers.AutomationPeer..cctor ()

I also found a workaround, but it doesn't work: http://blogs.msdn.com/b/jpvsblog/archive/2015/03/19/automationpeer-initialize-nre.aspx . The same exception occurs on this line:

 WindowAutomationPeer peer = new WindowAutomationPeer(m_wnd); 

      

+3


source to share





All Articles