我有一個.Net 2.0 C#WinForms應用程序在開發中運行良好,但偶爾會凍結在生產中。我已經找過所有通常的肇事者:
1.我用InvokeRequired和Invoke來確保沒有UI組件被錯誤的線程操縱。
2.我已經同步了UI線程上的所有計時器對象。
3.我讀過關於OnUserPreferenceChanged的這篇文章http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html,我確實有這個問題,但我解決了它。我的部分是什麼愚蠢的錯誤導致我的C#WinForms應用程序凍結/掛起?
當我們從.net 1.1遷移到.Net 2+時,就開始發生這種情況。在此之前,它永遠不會掛。
我使用託管堆棧資源管理器來定期從生產應用程序獲取堆棧跟蹤發生此問題時,主UI線程始終卡在一些新的隨機地點。它不會像WaitOne那樣被阻塞,因爲如果這是一個線程死鎖,您可能會期望。
我的應用程序做這些事情:
- 使用多個System.Timers.Timer實例。
- 使用WMI管理打印。
- 有幾個後臺線程可以讓Web服務調用服務器,但不會與UI交互。
- 有幾個後臺線程使用System.Diagnostics.Process來查看計算機上的其他進程,有時會終止它們。
- 與第三方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)
什麼我應該做的有什麼建議?
用hang表示一個沒有響應的用戶界面?或者是否顯示了一些錯誤消息? – CodingBarfield 2011-01-19 16:04:18
@沒有人認爲沒有看到所有的代碼(這是超出了SO的範圍),任何人都不能給你任何具體的導致你的應用程序掛起的建議。 – msarchet 2011-01-19 16:07:28