【发布时间】:2013-11-11 17:46:25
【问题描述】:
我的代码已死锁。这是主线程的堆栈跟踪:
[托管到本地转换]
WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetMessageW(ref System.Windows.Interop.MSG msg, System.Runtime.InteropServices.HandleRef hWnd,int uMsgFilterMin,int uMsgFilterMax) + 0x14 字节
WindowsBase.dll!System.Windows.Threading.Dispatcher.GetMessage(参考 System.Windows.Interop.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(对象 忽略)+ 0x17 字节
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window 窗口)+ 0x6f 字节
PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window 窗口) + 0x26 字节
PresentationFramework.dll!System.Windows.Application.Run() + 0x1b 字节 MainDashboard.exe!MainDashboard.App.Main() + 0x59 字节 C# [本地到托管转换] [托管到本地转换]
mscorlib.dll!System.AppDomain.ExecuteAssembly(字符串程序集文件, 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(object 状态)+ 0x6f 字节
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 state, bool preserveSyncCtx) + 0xa7 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 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 bytes [本机到托管转换]
我如何知道我的代码的哪一部分负责。应用入口点是:
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 回调、对象 state, bool preserveSyncCtx) + 0xa7 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 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 bytes [本机到托管转换]
这里是最后一个线程的调用栈:
[托管到本地转换]
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(对象 状态)+ 0x6f 字节
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 state, bool preserveSyncCtx) + 0xa7 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext、System.Threading.ContextCallback 回调、对象 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 bytes [本机到托管转换]
应用已挂起。它总共需要处理 49,000 条记录。它停在3,029。该应用程序此时未使用任何资源。任务管理器显示它使用 0% CPU。 UI 是响应式的,但它的设计始终是响应式的。
此外,这不是确定性的。我的意思是,如果我重新启动应用程序,它会在代码中的不同位置挂起,因此没有一条记录存在导致整个事情崩溃的问题。
【问题讨论】:
-
堆栈跟踪如何查找其他线程?
-
某些版本的 Visual Studio 有一个并发可视化工具,应该可以做你想做的事。
-
@kkokosa,我用其他调用堆栈编辑了这个问题。
-
处理已停止的事实并不意味着存在任何死锁。这可能与您的主处理循环不正确地获取
break一样简单,或者引发了一些异常并且一些外部try/catch已跳出您的主处理循环并因此停止了该过程。如果您连接了调试器,请检查输出是否有任何first-chance exception通知。任何 NullReference、ArgumentOutOfRange、NotImplemented、InvalidOperation 等都可能是错误处理错误的标志,只会停止循环。 -
将
try ... catch放入您的线程并让他们记录异常情况如何?
标签: c# .net multithreading task-parallel-library