Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.0 WPF Automation Exception

I have a project that was working fine in .NET 3.5 SP1. Now when upgrading to .NET 4.0 I have an Automation exception occurring.

I have searched my whole project for anything related to Automation and there is nothing to do with Automation. Also a Google search doesn't help with confirming if this is a bug. The error only occurs on a few PCs and it occurs randomly. It is possible to completely disable Automation as I think it might be a .NET 4.0 bug?

Exception Source: PresentationCore
Message: Object reference not set to an instance of an object.
Stack Trace:
   at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdatePeer(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, 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.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
like image 558
Luke Avatar asked Jun 17 '11 00:06

Luke


2 Answers

The EnsureChildren method is fairly simple:

private void EnsureChildren()
{
    if (!this._childrenValid || this._ancestorsInvalid)
    {
        this._children = this.GetChildrenCore();
        if (this._children != null)
        {
            int count = this._children.Count;
            for (int i = 0; i < count; i++)
            {
                this._children[i]._parent = this;
                this._children[i]._index = i;
                this._children[i]._hwnd = this._hwnd;
            }
        }
        this._childrenValid = true;
    }
}

The only chance for a NullReferenceException is with the this.children[i] code. The GetChildrenCore is usually implemented by the automation peers for custom WPF controls. So chances are one of those is return null in the collection returned from GetChildrenCore.

If you have any custom controls or any third party controls that implement a custom automation peer, then that would be a likely suspect.

You can disable UI automation on an element-by-element basis by create a custom class and overriding OnCreateAutomationPeer and returning null. That's the only way I know to disable automation.

Your best bet would probably be to remove various elements of your UI to narrow down which control is causing the issue.

like image 89
CodeNaked Avatar answered Nov 10 '22 18:11

CodeNaked


I had a similar problem and the following thread helped me: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/0ee9954d-0df5-4d61-8dc9-eb50c7a5be99.

Change the DataTemplate from "DayTitleTemplate" to "{x:Static CalendarItem.DayTitleTemplateResourceKey}"

like image 43
Friggers Avatar answered Nov 10 '22 18:11

Friggers