【问题标题】:System.Threading.ThreadAbortException in System.Windows.Markup.WpfXamlLoader.Load when application is restarted重新启动应用程序时 System.Windows.Markup.WpfXamlLoader.Load 中的 System.Threading.ThreadAbortException
【发布时间】:2011-12-07 16:10:59
【问题描述】:

我创建了一个在 Windows 启动时启动的 WPF 应用程序。应用程序检查系统上的文件列表,如果有任何文件丢失,它会弹出文件路径。

我面临的问题是当我在应用程序运行时重新启动系统时,在重新启动时我的应用程序崩溃并抛出 System.Threading.ThreadAbortException。下面是我在事件日志中得到的堆栈跟踪

Application: FileValidator.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Threading.ThreadAbortException
Stack:
   at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
   at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
   at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext)
   at System.Windows.Application.LoadComponent(System.Uri, Boolean)
   at System.Windows.Application.DoStartup()
   at System.Windows.Application.<.ctor>b__1(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at System.Windows.Application.Run(System.Windows.Window)
   at System.Windows.Application.Run()
   at FileValidator.App.Main()

这是什么问题,有没有人遇到过这个问题???

【问题讨论】:

  • windows重启时应用是如何启动的?你能在调试器下运行它并单步执行吗?
  • @Russell:我正在创建注册表项以在 Windows 启动时启动应用程序。我可以在调试器下运行它,但由于问题仅在机器重启后发生,所以在调试器下运行没有意义。我的应用程序最初运行良好,仅在重新启动机器后才会出现问题。
  • 您使用的是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 键吗?
  • 是的,我正在使用这个密钥
  • 你在使用文件系统监视吗?请发布代码以实时验证文件。

标签: wpf xaml threadabortexception


【解决方案1】:

如果没有更多的调试信息或代码可供查看,就不可能做出明确的回答,但这听起来像是 CLR 的问题。例如,如果您的应用程序在 CLR 运行时完全初始化之前正在执行,您可能会遇到一个异常,就像您在这里遇到的那样。由于 WPF 主要是托管代码,我认为这是一种明显的可能性。

我建议您将启动条目从 HKLM 移至 HKCU。这样,它应该在用户登录时加载,而不是在 Windows 启动时加载,这应该意味着届时 CLR 将被初始化。

如果这不起作用,MSDN 建议您对 ThreadAbortExceptions 执行 try/catch。如果 WPF 支持处理,我不记得我的头顶,但如果是这样,那么我会说如果将执行移动到引导堆栈的用户登录部分不能解决它,那将是你最好的选择。

有用的来源:

http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx

Is CLR loaded and initialized everytime,when a new managed application is loaded?

【讨论】:

  • 是的,毫无疑问。 WPF 可以很好地处理异常。将异常写入日志文件或事件日志。
  • @kris 和 xcalibur37:我在每个级别都进行了异常处理,但只是在任何级别都没有捕获到异常。所以,我假设我的应用程序没有出现异常。我将尝试将我的启动条目从 HKLM 移动到 HKCU 并让您获得结果。
  • @kris:将 statup 条目从 HKLM 移动到 HKCU 后,行为没有变化。机器重启后我仍然收到错误消息。
【解决方案2】:

你能检查更多细节吗

  • 有内部异常吗?
  • 系统事件日志中是否有任何条目。
  • 请设置性能计数器并检查 clr 是否有任何故障 (perfmon.exe)
  • 希望您了解 DispatcherUnhandledException 等 Application 类中的事件。请订阅这些事件。这将有助于捕获 try{}catch{}
  • 中遗漏的异常

【讨论】:

  • 我在问题中提供的堆栈跟踪来自事件日志。我会看看 DispatcherUnhandledException,我不知道。
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多