14

I am experiencing a very tricky defect in my WPF application to track down. The error message is:

An infinite loop appears to have resulted from repeatedly invalidating the TimeManager during the Layout/Render process.

The stack trace (for what it's worth) is:

at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 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)

This is an intermittent defect, and the only place I can ever catch it is in the app config file where I am trapping the Application_DispatcherUnhandledException message. Everything I have in my app is wrapped in try catch blocks and yet this winds up in the place for catching unhandled exceptions.

Does anyone have any insight into this? I've searched the internet for something and have found nothing and thought maybe someone here might have some insight or ideas how to track this down. Currently, I am swallowing this exception and letting the app continue to run as it does not seem to have any effect on it (other than crashing it).

2
  • Is it failing on all machines (in all environments)? Do you have the latest version of WPF including all hot fixes?
    – Halvard
    Commented Feb 18, 2013 at 18:38
  • Failing on all machines in all environments and yes we are up to date on all service packs. However we are using version 4.0 of .NET and not 4.5, so we don't have the latest version of WPF. We are not likely to upgrade to 4.5 anytime soon. Commented Feb 19, 2013 at 18:21

4 Answers 4

4

It is to be expected that you won't be able to catch the exception in the code : your bug comes from the Xaml part of your application, from an Animation most probably.

So, wether you create animation within Xaml or using code, you are, for instance, triggering animation A to start when animation B stops, AND Animation B to stop when animation A starts. So you have an endless loop : A Start -> B stops -> A Start -> B stop -> ...

There are many endless loop scenario possible in fact. They might be triggered by a CurrentStateInvalidated handler OR a Completed handler, OR a CurrentTimeInvalidated, and i might forget some scenarios using other types of triggers (mouse, ...) and/or the three previously mentionned. Possibly the code is too complex.

Remove all animations to test this scenario.

Try to have a clear way to reproduce the bug and check which handlers might be involved in such a loop and calling themselves one another endlessly.

... you might also use a counter within the handlers and have, for instance, a checkBox warn you that a given (big) number of calls was reached. This checkBox would also give the handler's name. click ok several time to check the loop 'members'.
You can keep a similar code in the release version and have a log file written, only once, when that number is reached, then the handler would always return before doing anything. Better than a crash.

... or just a code review might show you the possible loop(s).

Hope this helps.

EDIT : please please listen to my advice :

A ) remove all animations, test the application, and see what happens. B ) If you see no more bug : this is the cause.

C ) Then, in the anims, try removing the most 'dangerous' trigger, the event trigger. You can put XAML into comments by using , so just copy past the trigger after the animation.

D ) Test again : if you see no more bug, this is an event trigger E) Review all the event trigger and watch for a loop like i described above.

if in D) you still see the bug, repeat at C) with other triggers ( Property Trigger / Data Trigger )


Second though : it might be a Property / Data Trigger with some data changing very often...


Post some xaml maybe.


I had an idea : try to set a breakpoint in your Application_DispatcherUnhandledException. Then watch the inner exception's inner exception's ... until you reach the MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen. exception, then you can know the source. i wouldn't be surprised it is the Infragistics grid causing the issue. I once tested Telerik's control, and after fighting a few terrible issues, i just went back on a classical grid that i customized myself : both money and time were saved. I watched quickly for this grid : they are like issues like yours with converters or custom styles with this grid. --> try, if possible, a standard DataGrid.
--> if there's no bug, we have the root cause : try a 'bare' Infragistic grid, then test, then add style, then test. I am not sure you could test with no converters. Still it is always possible to have the view model expose the 'converted' properties...

Again : in your post, quote your some xaml, describe the fail-cases.

8
  • Vincent, thank you for taking the time to answer my question. Have you ever experienced exception before? My situation is that I am working with a graphic designer who knows how to code in xaml, but is not technically inclined. Commented Feb 12, 2013 at 19:31
  • Thank you Vincent for your additional comments. Have you ever experienced this exception before? Commented Feb 13, 2013 at 1:12
  • I just got this exception again and there was no animation involved. I was closing a dialog box that had an Infragistics grid, a text box and four buttons. The exception was thrown as I was closing the dialog box. Commented Feb 13, 2013 at 6:19
  • Ok i am sorry if i was misleading. But, by any chance (and after maybe an another direction is to be seeked) : does that grid you mention involves an animation OR the closing of the grid involves an animation in the parent window ? Commented Feb 13, 2013 at 20:37
  • Vincent, I did not want my question about whether or not you had ever experienced this issue directly to suggest you were misleading me. Not at all, I welcomed your willingness to jump in and help. I just wanted to know if this had ever happened to you before. When my graphic designer gets back from vacation, I am going to have her look into this from an animation perspective. The grid does not use animation directly, but that does not mean that the designer is not using it in the depths of xaml code. Commented Feb 14, 2013 at 0:23
2

I'm a bit late to the party, but hopefully this will help someone else in the future.

I've experienced this issue recently, but after ruling out the typical animations per-se, I've tracked it down to something that I didn't consider an animation (which looking back was a mistake) - VisualStateManager with Storyboards with a single KeyFrame. But in a very very specific combination of different factors.

So let's start with my VisualStateGroup:

<VisualStateGroup x:Name="ExpansionStates">
  <VisualState x:Name="Expanded">
    <Storyboard>
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                     Storyboard.TargetName="ItemsHost">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
      </ObjectAnimationUsingKeyFrames>
    </Storyboard>
  </VisualState>
  <VisualState x:Name="Collapsed" />
</VisualStateGroup>

As you can see, it seemed quite simple. But here are the factors that complicated this very much and caused the mentioned issue in my scenario:

  • The items which had the VisualStateManager states were part of a custom control with VirtualizingStackPanel.IsVirtualizing="True" and VirtualizingStackPanel.VirtualizationMode="Recycling"
  • The ItemsHost visibility was changed in the Visual State. This also caused the actualHeight and actualWidth of the element to change.

After switching to VirtualizationMode="Standard", the crashing went away but I saw what was probably causing it under the hood - some of the items in my listView were getting rendered on top of each other if you were scrolling through them quickly, creating a very artsy (but broken) collage. However, it didn't cause a crash and was resolving itself after a split second.

I dug a bit deeper and found that going back to the Ol' reliable <Trigger>s for changing the Visibility instead of using the VisualStateGroup resolved all the issues.

So to sum it up: If you get here with the same issue and are using VisualStateManager inside something that has virtualization, and the States can change the actual height/width of the rendered items, this can in some cases lead to this hard to debug scenario. Consider switching to Triggers for the debugging!

-2
+50

At times exceptions are very tricky. One of the suggestion is to have a catch block without the exception object. So you can update your catch block from:

 catch(Exception ex)
{
..
}

to

    catch()
{
..
}

There are other low level exceptions that are difficult to catch, as mentioned in the reply by Peli

5
  • Avani, thank you for taking the time to answer my question. I am interested in your thoughts about how using an empty catch will help me track my issue down? Commented Feb 13, 2013 at 23:07
  • Hi MikeMalter, sorry for the late reply. The empty catch might help you in narrowing down to that region of your code that is throwing exception. Commented Feb 16, 2013 at 7:20
  • How does an empty catch do that? Thanks. Commented Feb 19, 2013 at 20:03
  • 1
    I do get your point, catch(Exception) is only catching an exception of type System.Exception and since I have everything wrapped up in try/catch(Exception) blocks and since the TimeManager exception never gets rolled up into a System.Exception, if I am catching exceptions without arguments I might catch the TimeManager exception. The problem is that the exception is intermittent and happens at random locations under random conditions and re-coding my application to trap this is not an option at this point. However, your answer is the best so far so you get the bounty. Commented Feb 19, 2013 at 20:30
  • 1
    There is no such thing as an exception that doesn't inherit from System.Excetpion in .NET land. This answer is all wrong. referencesource.microsoft.com/#PresentationCore/Core/CSharp/…
    – Brannon
    Commented May 30, 2018 at 21:41
-2

Have you already solved your problem? I was researching a little bit, and it sounds like you are using third-party controls from Infragistics right?

http://help.infragistics.com/doc/WinForms/2014.2/CLR4.0/?page=Infragistics4.Win.UltraWinStatusBar.v14.2~Infragistics.Win.UltraWinStatusBar.TimerManager.html

Were they able to help you on this issue?

2
  • Rodrigo, Your link is for WinForms and my question pertains to WPF. I looked at the link you posted and it is not relevant. Commented Oct 18, 2014 at 2:12
  • I understand that, and that is why I thought you should look at. There isn't any referente to TimerManager class in WPF. Any chance you are using a wrong reference or something that might be crashing your WPF using WinForms classes?
    – rodrigogq
    Commented Oct 19, 2014 at 4:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.