Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this questionI have a .Net 2.0 C# WinForms application that runs fine in development but occasionally freezes in production. I have looked for all the usual culprits:
1. I've used InvokeRequired and Invoke to make sure that no UI components are manipulated from the wrong thread. 2. I've synchronized all my timer objects on the UI thread. 3. I've read this article http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html about OnUserPreferenceChanged and I did have this problem, but I fixed it.This started happening when we migrated from .Net 1.1 to .Net 2+. Before that, it never hang.
I'm using Managed Stack Explorer to periodically get stack traces from the production application when this problem occurs and the main UI thread is always stuck in some new random place. It's not blocked on WaitOne as you might expect if this were a thread deadlock.
My application does these things:
- Uses multiple System.Timers.Timer instances. - Uses WMI to manage printing. - Has a few background threads that make Web Services calls to a server, but do not interact with the UI. - Has a few background thread that use System.Diagnostics.Process to look at other processes on the computer and sometimes kill them. - Interacts with a 3rd party dll that uses the serial port to talk to a bill acceptor. - Interacts with some com/activex/ocx controls to interact with a credit card reader.Here's an example of one time it hang and all the stack traces at the time:
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::DispatchMessag开发者_运维问答eW] (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)
Any suggestions on what I ought to do?
Some thoughts:
Are you using any preprocessor flags in your Development environment that don't exist in production. So any
#If Debug
or whatever.Have you been testing on the development environment in release mode? Does it still not hang?
What's different about the Machines? Does one have more RAM, a quad-core vs. dual-core, is one behind a million firewalls and the other not?
In your threading are you sure nothing is ever waiting on some call back to finish before updating a UI element?
Are you using databases? If so are they different ones and how are they different?
Is the hanging maybe caused by the fact that you are pulling StackTraces?
Are there any exceptions that you are swallowing anywhere that may be telling you what is happening?
Is there something different about the Serial Port on the machines?
Is the same bill receptor being used?
Continue and so on.
Try installing this on a fresh VM and see what happens. It could be some old dll or driver, most of the time when Works on my machine
symptoms happen it's because there was something you did that you forgot about that made things work.
We had the exact same problem, and this article (like you mentioned) pointed us to the solution: http://www.ikriv.com/dev/dotnet/MysteriousHang.html#WhatToDo
We used a splash screen with the following code:
ThreadPool.QueueUserWorkItem((x) =>
{
using (var splashForm = new SplashForm())
{
splashForm.Show();
while (!done)
Application.DoEvents();
splashForm.Close();
}
});
We had the luck we could reproduce the deadlock. We have a laptop/tablet where you can detach the screen from the keyboard so you have a tablet only. Every time we detached the screen, our application hung. After removing the splash our problem was gone.
We had the exact same stack trace from the hanging program by the way:
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)
Offcourse I don't know if your problem has the same cause/solution... But this did it for us.
I have seen a similar-looking hang, where one thread was making that call to Application.get_ComCtlSupportsVisualStyles .
I wonder if you have enabled visual styles? http://ryanfarley.com/blog/archive/2004/05/05/599.aspx describes how to do it using either code or manifest file. (I think the manifest file is the preferred solution.)
If you get visual styles enabled, and assuming the OS supports themes (I think this feature started with XP), then Application.get_ComCtlSupportsVisualStyles does not have to do nearly as much work, and can return true quickly.
All that said, I still don't understand why the call to GetModuleHandle() would hang. Most of what I have found on the web about indicates it would be a problem during LoadLibrary(), not during a .NET Win Forms paint event. I don't know if maybe you have another thread that is at the same time doing unsafe work in LoadLibrary()...?
精彩评论