I have a WPF application with a bunch of user controls, pages etc. When i deploy my app and run it, sometimes it will just crash and close. Very intermittent. I have to go to the Event viewer and look at the Windows logs to see what happened. I will put a condensed version of the error found in the logs:
Application: MyApp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
at System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource)
at System.Nullable`1[[System.TimeSpan, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]].get_Value()
at MyApp.UserControls.UserCountdown+<>c__DisplayClass13_0.<StartAnimation>b__0(
I see my function StartAnimation is where its bombing, but I have that code in a try catch block:
public void StartAnimation()
{
try
{
//my code
}
catch (Exception ex){
//gracefully handle this
}
}
The Windows logs error doesn't display a line number or anything. I'm not looking for coding, I am wanting to know how can i stop my application from just totally crashing and actually land in my try catch blocks instead of the Event Viewer?
P.S. Here's my entire function. Not sure if this will help:
public void StartAnimation()
{
try
{
double from = -90;
double to = 270;
int seconds = Seconds;
int newSecs = seconds;
if (Seconds > 60)
newSecs = newSecs / 2;
TimeSpan duration = TimeSpan.FromSeconds(Seconds);
storyboard = new Storyboard();
DoubleAnimation animation = new DoubleAnimation(from, to, new Duration(duration));
Storyboard.SetTarget(animation, Arc);
Storyboard.SetTargetProperty(animation, new PropertyPath("EndAngle"));
storyboard.CurrentTimeInvalidated += (s, e) =>
{
int diff = (int)((s as ClockGroup).CurrentTime.Value.TotalSeconds);
Seconds = seconds - diff;
TimeSpan t = new TimeSpan(0, 0, Seconds);
if (t.Hours > 0)
{
timeLabel.FontSize = 16;
TimeDisplay = $"{t.Hours.ToString("D2")}:{t.Minutes.ToString("D2")}:{t.Seconds.ToString("D2")}";
}
else if (t.Minutes > 0)
{
timeLabel.FontSize = 25;
TimeDisplay = $"{t.Minutes.ToString("D2")}:{t.Seconds.ToString("D2")}";
}
else
{
timeLabel.FontSize = 30;
TimeDisplay = $"{t.Seconds.ToString("D2")}";
}
};
storyboard.Children.Add(animation);
storyboard.Begin();
}
catch (Exception ex)
{
Emailer.SendEmail($"Error calling StartAnimation - {ex.StackTrace}", ex.Message, "", "", "");
}
}