【发布时间】:2011-01-19 15:54:57
【问题描述】:
我有一个 .Net 2.0 C# WinForms 应用程序,它在开发中运行良好,但在生产中偶尔会冻结。我已经找到了所有常见的罪魁祸首:
1. 我使用 InvokeRequired 和 Invoke 来确保没有 UI 组件是从错误的线程操作的。
2. 我已经在 UI 线程上同步了我的所有计时器对象。
3. 我已经阅读了这篇关于 OnUserPreferenceChanged 的文章http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html,我确实遇到了这个问题,但我已经解决了。
当我们从 .Net 1.1 迁移到 .Net 2+ 时,这种情况就开始发生了。在此之前,它永远不会挂起。
当出现此问题并且主 UI 线程总是卡在某个新的随机位置时,我正在使用托管堆栈资源管理器定期从生产应用程序获取堆栈跟踪。如果这是线程死锁,它不会在 WaitOne 上被阻塞,正如您所期望的那样。
我的应用程序执行以下操作:
- 使用多个 System.Timers.Timer 实例。
- 使用 WMI 管理打印。
- 有一些后台线程可以对服务器进行 Web 服务调用,但不与 UI 交互。
- 有一些后台线程使用 System.Diagnostics.Process 来查看计算机上的其他进程,有时会杀死它们。
- 与第 3 方 dll 交互,该 dll 使用串行端口与票据接受者通信。
- 与一些 com/activex/ocx 控件交互以与信用卡读卡器交互。
以下是它一次挂起以及当时所有堆栈跟踪的示例:
Thread ID: 2816
0. System.Threading.WaitHandle.WaitOne (Source Unavailable)
1. System.Threading.WaitHandle.WaitOne (Source Unavailable)
2. System.Threading.WaitHandle.WaitOne (Source Unavailable)
3. System.Management.MTAHelper.WorkerThread (Source Unavailable)
4. System.Threading.ThreadHelper.ThreadStart_Context (Source Unavailable)
5. System.Threading.ExecutionContext.Run (Source Unavailable)
6. System.Threading.ThreadHelper.ThreadStart (Source Unavailable)
Thread ID: 2828
Thread ID: 2852
0. MeiNet.EBDS.BillAcceptor.submitRequestStack (Source Unavailable)
1. MeiNet.EBDS.BillAcceptor.OnHandleEscrowed (Source Unavailable)
2. MeiNet.EBDS.BillAcceptor.EventHandler (Source Unavailable)
Thread ID: 2908
Thread ID: 2820
0. System.Threading.Thread.Sleep (Source Unavailable)
1. MyApp.ProcessKiller.startSpecialKillerLoop (ProcessKiller.cs:115)
2. System.Threading.ThreadHelper.ThreadStart_Context (Source Unavailable)
3. System.Threading.ExecutionContext.Run (Source Unavailable)
4. System.Threading.ThreadHelper.ThreadStart (Source Unavailable)
Thread ID: 2600
Thread ID: 2844
0. System.Threading.Thread.Start (Source Unavailable)
1. System.Net.TimerThread.Prod (Source Unavailable)
2. System.Net.TimerThread.TimerQueue.CreateTimer (Source Unavailable)
3. System.Net.ServicePoint..ctor (Source Unavailable)
4. System.Net.ServicePointManager.FindServicePointHelper (Source Unavailable)
5. System.Net.ServicePointManager.FindServicePoint (Source Unavailable)
6. System.Net.HttpWebRequest.FindServicePoint (Source Unavailable)
7. System.Net.HttpWebRequest.GetRequestStream (Source Unavailable)
8. System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke (Source Unavailable)
9. MyApp.MyServer.Server.hb4 (Reference.cs:588)
10. MyApp.HeartbeatSender.SendThreadedHeartbeat (HeartbeatSender.cs:81)
11. MyApp.HeartbeatSender.StartHeartbeatLoop (HeartbeatSender.cs:40)
12. System.Threading.ThreadHelper.ThreadStart_Context (Source Unavailable)
13. System.Threading.ExecutionContext.Run (Source Unavailable)
14. System.Threading.ThreadHelper.ThreadStart (Source Unavailable)
Thread ID: 2824
0. System.Threading.Thread.Sleep (Source Unavailable)
1. MyApp.OfficeMonitor.StartMonitoring (OfficeMonitor.cs:84)
2. System.Threading.ThreadHelper.ThreadStart_Context (Source Unavailable)
3. System.Threading.ExecutionContext.Run (Source Unavailable)
4. System.Threading.ThreadHelper.ThreadStart (Source Unavailable)
Thread ID: 2584
0. [Internal thisFrame, 'M-->U', System.Windows.Forms.UnsafeNativeMethods::GetModuleHandle] (Source Unavailable)
1. System.Windows.Forms.Application.get_ComCtlSupportsVisualStyles (Source Unavailable)
2. System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintWorker (Source Unavailable)
3. System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintUp (Source Unavailable)
4. System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintOver (Source Unavailable)
5. System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.Paint (Source Unavailable)
6. System.Windows.Forms.ButtonBase.OnPaint (Source Unavailable)
7. System.Windows.Forms.Control.PaintWithErrorHandling (Source Unavailable)
8. System.Windows.Forms.Control.WmPaint (Source Unavailable)
9. System.Windows.Forms.Control.WndProc (Source Unavailable)
10. System.Windows.Forms.ButtonBase.WndProc (Source Unavailable)
11. System.Windows.Forms.Button.WndProc (Source Unavailable)
12. System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Source Unavailable)
13. System.Windows.Forms.Control.ControlNativeWindow.WndProc (Source Unavailable)
14. System.Windows.Forms.NativeWindow.Callback (Source Unavailable)
15. [Internal thisFrame, 'M-->U', System.Windows.Forms.SafeNativeMethods::UpdateWindow] (Source Unavailable)
16. System.Windows.Forms.Control.Update (Source Unavailable)
17. System.Windows.Forms.Control.Refresh (Source Unavailable)
18. MyApp.MainForm.ClearFields (MainForm.cs:4057)
19. MyApp.MainForm.ShowHomeButtons (MainForm.cs:5664)
20. MyApp.MainForm.ShowScreenSaver (MainForm.cs:4284)
21. MyApp.MainForm.ScreensaverTimerTick (MainForm.cs:7817)
22. System.RuntimeMethodHandle.InvokeMethodFast (Source Unavailable)
23. System.Reflection.RuntimeMethodInfo.Invoke (Source Unavailable)
24. System.Delegate.DynamicInvokeImpl (Source Unavailable)
25. System.Windows.Forms.Control.InvokeMarshaledCallbackDo (Source Unavailable)
26. System.Windows.Forms.Control.InvokeMarshaledCallbackHelper (Source Unavailable)
27. System.Threading.ExecutionContext.runTryCode (Source Unavailable)
28. System.Threading.ExecutionContext.RunInternal (Source Unavailable)
29. System.Threading.ExecutionContext.Run (Source Unavailable)
30. System.Windows.Forms.Control.InvokeMarshaledCallback (Source Unavailable)
31. System.Windows.Forms.Control.InvokeMarshaledCallbacks (Source Unavailable)
32. System.Windows.Forms.Control.WndProc (Source Unavailable)
33. System.Windows.Forms.ScrollableControl.WndProc (Source Unavailable)
34. System.Windows.Forms.ContainerControl.WndProc (Source Unavailable)
35. System.Windows.Forms.Form.WndProc (Source Unavailable)
36. System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Source Unavailable)
37. System.Windows.Forms.Control.ControlNativeWindow.WndProc (Source Unavailable)
38. System.Windows.Forms.NativeWindow.Callback (Source Unavailable)
39. [Internal thisFrame, 'M-->U', System.Windows.Forms.UnsafeNativeMethods::DispatchMessageW] (Source Unavailable)
40. System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Source Unavailable)
41. System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Source Unavailable)
42. System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Source Unavailable)
43. System.Windows.Forms.Application.Run (Source Unavailable)
44. MyApp.MainForm.Main (MainForm.cs:2975)
对我应该做什么有什么建议吗?
【问题讨论】:
-
挂起是指不再响应的用户界面吗?还是显示了一些错误信息?
-
@Nobody 我认为没有人可以在不查看所有代码的情况下就具体导致您的应用程序挂起的原因向您提供任何建议(这超出了 SO 的范围)。跨度>
-
死锁不仅仅发生在您告诉某事等待时。如果您正在锁定某个东西,而另一个线程去锁定它,这可能会导致死锁,如果一个方法依赖于它尚未初始化的东西并且需要锁定它可能会导致死锁。等等等等等等。当你使用线程时要super小心。
-
没有错误信息。用户界面变得无响应。
-
我应该补充一点,这种挂起是在我们从 .Net 1.1 迁移到 .Net 2+ 之后开始发生的。在此之前从未发生过。
标签: c# multithreading winforms