What stupid mistake on my part is causing my C # WinForms application to freeze / freeze?

I have a .NET Net C # WinForms application that works great in development, but freezes from time to time during production. I was looking for all ordinary criminals:
1. I used InvokeRequired and Invoke to make sure that no user interface components are being controlled due to incorrect flow.
2. I synchronized all my timer objects in the user interface thread.
3. I read this article http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html about OnUserPreferenceChanged, and I had this problem, but I fixed it.

It started when we migrated from .Net 1.1 to .Net 2+. Before that, he never hangs.

I use Managed Stack Explorer to periodically retrieve stack traces from a production application when this problem occurs, and the main UI thread always gets stuck in some new random place. It is not blocked in WaitOne, as you would expect if it were a dead end.

My application does the following:
- Uses multiple instances of System.Timers.Timer.
- Uses WMI to manage print.
- It has several background threads that make web service calls on the server, but do not interact with the user interface.
- It has several background threads that use System.Diagnostics.Process to look at other processes on the computer and sometimes kill them.
- Interacts with a third-party dll using a serial port to talk to a bill acceptor.
- Interacts with some com / activex / ocx commands to interact with a credit card reader.

Here is an example of one time when it freezes and all the stack traces at this 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::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) 

Any suggestions on what I should do?

+4
source share
3 answers

Some thoughts:

  • Do you use any preprocessor flags in your development environment that do not exist in production. So, any #If Debug or something else.

  • Have you tested the development environment in release mode? Is he still not hanging?

  • What is the difference between cars? Do you have more RAM, a quad-core processor and a dual-core, one after a million firewalls and the other not?

  • Are you sure in your streaming that no one needs to wait for any call to complete before updating the user interface element?

  • Do you use databases? If so, are they different and how are they different?

  • Is this possible because you are pulling out StackTraces?

  • Are there any exceptions that you swallow anywhere where it can be said what is happening?

  • Is there anything else in Serial Port on machines?

  • Is the same account receptor used?

Continue etc.

Try installing this on a new virtual machine and see what happens. It may be some old dll or driver, most of the time when the symptoms of Works on my machine are due to the fact that you did something, you forgot about it, which made everything work.

+2
source

We had the same problem, and this article (as you mentioned) pointed us to a solution: http://www.ikriv.com/dev/dotnet/MysteriousHang.html#WhatToDo

We used a screensaver with the following code:

 ThreadPool.QueueUserWorkItem((x) => { using (var splashForm = new SplashForm()) { splashForm.Show(); while (!done) Application.DoEvents(); splashForm.Close(); } }); 

We are fortunate that we could reproduce the impasse. We have a laptop / tablet where you can disconnect the screen from the keyboard so that you only have a tablet. Every time we took a screen, our application crashed. After removing the surge, our problem disappeared.

We had exactly the same stack trace from a hanging program:

 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 do not know if your problem has the same reason / solution ... But it did it for us.

+1
source

I saw a similar view where one thread called this call to Application.get_ComCtlSupportsVisualStyles.

I wonder if you included visual styles? http://ryanfarley.com/blog/archive/2004/05/05/599.aspx describes how to do this using either code or a manifest file. (I think the manifest file is the preferred solution.)

If you turned on visual styles and assumed that the OS supports themes (I think this function started with XP), then Application.get_ComCtlSupportsVisualStyles does not need to do almost as much work, and can quickly return to its original state.

All that said, I still don't understand why the call to GetModuleHandle () will hang. Most of what I found on the Internet indicates that this will be a problem during LoadLibrary (), and not during the .NET Win Forms drawing event. I don’t know, maybe you have another thread that at the same time does unsafe work in LoadLibrary () ...?

0
source

Source: https://habr.com/ru/post/1336163/


All Articles