【发布时间】:2021-02-11 21:44:34
【问题描述】:
我有一个带有大量用户控件、页面等的 WPF 应用程序。 当我部署我的应用程序并运行它时,有时它会崩溃并关闭。很断断续续。我必须转到事件查看器并查看 Windows 日志以查看发生了什么。我将把发现的错误的精简版放在日志中:
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(
我看到我的函数 StartAnimation 是它的炸弹,但我在 try catch 块中有该代码:
public void StartAnimation()
{
try
{
//my code
}
catch (Exception ex){
//gracefully handle this
}
}
Windows 日志错误不显示行号或任何内容。我不是在寻找编码,我想知道如何阻止我的应用程序完全崩溃并真正进入我的 try catch 块而不是事件查看器?
附:这是我的全部功能。不确定这是否会有所帮助:
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, "", "", "");
}
}
【问题讨论】:
-
当你调试电离发生前的最后一行是什么?
-
Red Herring 也许,检查你的“秒”和“秒” - TimeSpan 持续时间 = TimeSpan.FromSeconds(Seconds); ???
-
我假设两个代码摘录中的
Exception指的是System.Exception而不是其他类型?我知道这通常是这种情况,但由于它们似乎没有捕获所有(通常是可捕获的)异常,也许他们实际上正在捕获其他东西? -
问题所在的行可能是
int diff = (int)((s as ClockGroup).CurrentTime.Value.TotalSeconds);和CurrentTime可能是null
标签: c#