locked
How to catch unhandled exception in PresentationFramework? RRS feed

  • Question

  • In some rare cases I experience an unhandled exception in PresentationFramework.dll. The exception occurs in a custom control.

    (I'm using .Net 3.5 SP1)

     	PresentationFramework.dll!System.Windows.Controls.GridViewRowPresenter.OnLayoutUpdated(object sender, System.EventArgs e) + 0x33 bytes	
     	PresentationCore.dll!System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() + 0xb8 bytes	
     	PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout() + 0x641 bytes	
     	PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayoutCallback(object arg) + 0x19 bytes	
     	PresentationCore.dll!System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() + 0x10 bytes	
     	PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() + 0x97 bytes	
     	PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget = null) + 0x80 bytes	
     	PresentationCore.dll!System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(object resizedCompositionTarget) + 0x2b bytes	
     	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback = {Method = {System.Object AnimatedRenderMessageHandler(System.Object)}}, object args = null, bool isSingleParameter = true) + 0x8a bytes	
     	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate callback, object args, bool isSingleParameter, System.Delegate catchHandler = null) + 0x4a bytes	
     	WindowsBase.dll!System.Windows.Threading.Dispatcher.WrappedInvoke(System.Delegate callback, object args, bool isSingleParameter, System.Delegate catchHandler) + 0x44 bytes	
     	WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() + 0x5d bytes	
     	WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) + 0x38 bytes	
     	mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x51 bytes	
    

     

    What's best practice how to handle such kind of exceptions? I would like to handle them internally in my custom control...

    Tuesday, February 1, 2011 2:03 PM

Answers

  • I found a workaround my problem. I don't need any further help in this case (many thanks to Sheldon _Xiao). Nonetheless, I would like to share my solution...

     

    My custom control contains a grid view. Clicking on the auto-size column splitter caused the exception. The element in the column is a TextBox. I set the width of the TextBox as follows:

    Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=ActualWidth}"
    
    

    what set the width of the textbox to Double.NaN (I guess...)

    Anyway, I now changed removed this logic and added the following trigger:

    <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
    </ListView.ItemContainerStyle>
    

     

    It seems like the Double.NaN caused the exception. Because I now got rid of it the problem is solved.

    • Marked as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:28 AM
    Friday, February 4, 2011 1:22 PM

All replies

  • By the way that's the entire exception message:

    System.NullReferenceException occurred
     Message="Object reference not set to an instance of an object."
     Source="PresentationFramework"
     StackTrace:
        at System.Windows.Controls.GridViewRowPresenter.OnLayoutUpdated(Object sender, EventArgs e)
     InnerException: 
    

    Wednesday, February 2, 2011 1:29 PM
  • I made some small progress. I just added the following code:

        public MyCustomControl()
        {
          Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        }
    
        void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
          if (e.Exception != null && e.Exception.Source == "PresentationFramework")
          {
            e.Handled = true;
          }
        }
    
    Now my app doesn't crash anymore, but my control doesn't work instead... (if I set a breakpoint at e.Handled and get repeating hits...)

    • Proposed as answer by Sheldon _Xiao Friday, February 4, 2011 5:23 AM
    Thursday, February 3, 2011 12:55 PM
  • Hi Dunken,

    I am not sure the meaning of "my control doesn't work", could you share a sample with me to reproduce your issue?

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 4, 2011 5:25 AM
  • I extended a textbox similar to a combobox. You can press a button and a popup is displayed. After handling the error in my event handler every time I try to open up a new popup a new exception is thrown in the button-click event-handler...

    _popup.ClearValue(Popup.IsOpenProperty); // exception occurs
    IsDropDownOpen = true;
    
    How can I share my sample project?
    Friday, February 4, 2011 7:59 AM
  • I found a workaround my problem. I don't need any further help in this case (many thanks to Sheldon _Xiao). Nonetheless, I would like to share my solution...

     

    My custom control contains a grid view. Clicking on the auto-size column splitter caused the exception. The element in the column is a TextBox. I set the width of the TextBox as follows:

    Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=ActualWidth}"
    
    

    what set the width of the textbox to Double.NaN (I guess...)

    Anyway, I now changed removed this logic and added the following trigger:

    <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
    </ListView.ItemContainerStyle>
    

     

    It seems like the Double.NaN caused the exception. Because I now got rid of it the problem is solved.

    • Marked as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:28 AM
    Friday, February 4, 2011 1:22 PM
  • Hi Dunken,

    I am back, and I am glad to hear that your have resolved your issue.

    Thank you for sharing your solution here, I think it will be very beneficial for others having the similar issue to find the solution.

    I am marking your issue as "Answered", if you have any updates about your issue, please unmark.

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 9, 2011 2:28 AM
  • I have the same scenario, but the fix does not help. I already have HorizontalContentAlignment set to Stretch. My ListView has GridView, and grouping is enabled. It has > 500 items (so virtualization while grouped would be a great help, but that is another topic). For me to be able to have the content of each TextBlock in columns be able to wrap, I had to set the following on the panel templates on the ListView

     

    <ListView.GroupStyle>
    	<GroupStyle >
    		<GroupStyle.Panel>
    			<ItemsPanelTemplate>
    				<StackPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter }}}" />
    			</ItemsPanelTemplate>
    		</GroupStyle.Panel>
    	</GroupStyle>
    </ListView.GroupStyle>
    <ListView.ItemsPanel>
    	<ItemsPanelTemplate>
    		<VirtualizingStackPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter }}}" />
    	</ItemsPanelTemplate>
    </ListView.ItemsPanel>
    

     

    I assume this leads to the same issue as in Dunken's situation. Without this binding, I am not able to wrap content (inside columns or expander content).

    The thing is that this listview style is used on all listviews in my application, but this only fails for one list, and it does not fail every time. And it only started failing after we added all the 500 items to it. Also, it doesn't fail untill I select an item in the listview. Each item contains an Expander. It of course has to do with updating the layout of the items when selecting.

    Any suggestions are appreciated. I realize it is hard to help without a complete example. But the clue of Double.NaN is a possible great help.

     

     


    Regards, Bjørnar Sundsbø
    Tuesday, November 29, 2011 9:03 AM