1

我的代码已经死锁。这是主线程的堆栈跟踪:

[管理到本地转换]

WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetMessageW(ref System.Windows.Interop.MSG msg, System.Runtime.InteropServices.HandleRef hWnd, int uMsgFilterMin, int uMsgFilterMax) + 0x14 bytes
WindowsBase.dll!System.Windows.Threading。 Dispatcher.GetMessage(参考 System.Windows.Interop.MSG msg,System.IntPtr hwnd,int minMessage,int maxMessage)+ 0x80 字节
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame 帧) + 0x75 字节
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame 帧) + 0x49 字节
WindowsBase.dll!System.Windows.Threading.Dispatcher.Run() + 0x4b 字节
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) + 0x17 字节
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window 窗口) + 0x6f 字节
PresentationFramework.dll!System.Windows.Application。 Run(System.Windows.Window window) + 0x26 bytes
PresentationFramework.dll!System.Windows.Application.Run() + 0x1b bytes MainDashboard.exe!MainDashboard.App.Main() + 0x59 bytes C# [Native to Managed Transition] [管理到本机转换]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b 字节
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 字节 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态) + 0x6f 字节
mscorlib.dll!System.Threading。 ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading. ContextCallback 回调,对象状态,bool preserveSyncCtx) + 0x16 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes [Native到托管过渡]

我怎么知道我的代码的哪一部分负责。应用程序入口点是:

MainDashboard.exe!MainDashboard.App.Main() + 0x59 字节 C#

这是这个堆栈跟踪中唯一来自我的代码的行。

尝试查看其他两个帧的调用堆栈会显示以下内容:

当前线程不是当前正在运行的代码,所有调用堆栈都无法获取。

这是另一个工作线程的调用堆栈:

[管理到本地转换]

System.dll!Microsoft.Win32.SystemEvents.WindowThreadProc() + 0xaf 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态) + 0x6f 字节
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading. ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx ) + 0x16 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调,对象状态) + 0x41 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 字节 [本机到托管转换]

这是最后一个线程的调用堆栈:

[管理到本地转换]

Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.WaitForThreadExit() + 0x93 字节 Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunParkingWindowThread() + 0x253 字节 mscorlib。 dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0x16 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes [Native到托管过渡]

该应用程序已挂起。它总共需要处理 49,000 条记录。它停在3,029。该应用程序此时未使用任何资源。任务管理器显示它使用 0% CPU。UI 是响应式的,但它的设计始终是响应式的。

此外,这不是确定性的。我的意思是,如果我重新启动应用程序,它会挂在代码中的不同位置,因此没有一条记录存在导致整个事情崩溃的问题。

4

2 回答 2

4

由于您现在在“输出”中注意到“第一次机会异常”并且它们似乎会杀死您的线程,因此除了 try/catch/log 之外,还有一件事。

进一步使用 VS/CLR 的调试功能。转到 DEBUG 菜单,然后找到 EXCEPTIONS,然后找到一个(或全部,但我更喜欢一个接一个)您识别为 throw-and-killing-your-threads 的异常,即 InvalidOperationException 并勾选“throw” ”或“未处理”(取决于您想要什么以及您拥有的 VS 版本)。

现在,假设您勾选了“抛出”,IDE 将在尝试完全抛出这种类型的异常时自动中断/停止程序。

..它不仅会停止程序,还会像放置断点一样跳转到那里。您将立即获得您想要检查的所有堆栈跟踪、变量、代码等。

但是,另一方面,如果您的应用程序正在以每秒数千的速度发送这种异常的垃圾邮件,只是因为有人懒惰并且 if/else 有一些错误的参数案例并尝试/捕获异常,然后尝试依靠这个异常被“抛出”将..好吧..失败。在这种情况下,您可能想尝试使用“未处理”刻度(如果您在 IDE 中看到它),但当然它也有其自身的局限性。尽管如此,只要您可以将 IDE 附加到失败的进程,它们确实是非常宝贵的工具。Insta-break 在有问题的未知线路上!

于 2013-11-12T11:03:40.073 回答
-1

听起来您在识别应用程序中发生的事情并且没有日志记录时遇到了麻烦......

http://logging.apache.org/log4net/

在尝试解决这类问题时,日志文件非常宝贵。

实现 Log4net,将其配置为写入日志文件并开始记录内容......

例如,在您怀疑可能在方法开始时涉及的地方

private void MethodName()
{
    logger.Debug("Begin MethodName");

和方法的结束

    logger.Debug("End MethodName");

很快你就会得到你需要的线索。

您可以自定义实际记录的内容,以便获取详细的调试记录或仅记录错误。

此外,如果您还没有使用 Visual Studio 的“调试位置”工具栏,请使用它。

于 2013-11-12T01:39:44.410 回答