代码之家  ›  专栏  ›  技术社区  ›  dsum

Windows窗体应用程序在夜间运行时随机冻结

  •  8
  • dsum  · 技术社区  · 16 年前

    我有一个窗口窗体应用程序,它有多个线程在运行,这些线程将调用主UI线程来更新UI。偶尔在开发机器上,应用程序主UI线程将停止运行,应用程序不再响应。如果我让应用程序在一夜之间运行,那么这种情况似乎就会发生。但是,我有一些用户通过远程桌面运行这个窗口窗体应用程序,如果应用程序在没有用户交互的情况下运行一整夜,这个问题就会发生得更多。

    我找到了一个 article 似乎是在描述这个问题,但我没有足够的Windows开发知识来理解为什么应用程序会冻结。

    我得到的唯一信息是下面的堆栈跟踪,表明主UI线程正在等待某种操作。

    Main UI thread stack trace:
    
    mscorlib.dll!System.Threading.WaitHandle.WaitOne(long timeout, bool exitContext) + 0x2f bytes
    mscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) + 0x25 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle waitHandle = {System.Threading.ManualResetEvent}) Line 4268 C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control caller, System.Delegate method, object[] args, bool synchronous) Line 7614 C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method, object[] args) Line 7178 + 0x11 bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.WindowsFormsSynchronizationContext.Send(System.Threading.SendOrPostCallback d, object state) Line 89 C#
    System.dll!Microsoft.Win32.SystemEvents.SystemEventInvokeInfo.Invoke(bool checkFinalization = true, object[] args = {object[2]}) + 0x62 bytes
    System.dll!Microsoft.Win32.SystemEvents.RaiseEvent(bool checkFinalization = true, object key = {object}, object[] args = {object[2]}) + 0x10f bytes
    System.dll!Microsoft.Win32.SystemEvents.OnUserPreferenceChanging(int msg, System.IntPtr wParam, System.IntPtr lParam) + 0x77 bytes
    System.dll!Microsoft.Win32.SystemEvents.WindowProc(System.IntPtr hWnd = 2032836, int msg = 8218, System.IntPtr wParam = 47, System.IntPtr lParam = 100019840) + 0x2ca bytes
    [Native to Managed Transition]
    [Managed to Native Transition]
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = 4, int pvLoopData = 0) Line 2106 + 0x8 bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = 4, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.Application.ModalApplicationContext}) Line 3377 + 0x1b bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3261 + 0xa bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form form) Line 1488 C#
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) Line 6120 + 0x8 bytes C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleAppMainForm.ShowOpenScheduleForm.AnonymousMethod() Line 829 + 0xd bytes C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleAppMainForm.PromptUserToSaveSchedule(System.Action oAfterPromptUserToSaveCallBack = {Method = Cannot evaluate expression because the code of the current method is optimized.}) Line 1858 + 0xb bytes C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleAppMainForm.ShowOpenScheduleForm() Line 859 + 0xb bytes C#
    [Native to Managed Transition]
    [Managed to Native Transition]
    mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args) + 0x55 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackDo(System.Windows.Forms.Control.ThreadMethodEntry tme) Line 7266 + 0xb bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj) Line 7228 + 0x7 bytes C#
    mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x51 bytes
    [Native to Managed Transition]
    [Managed to Native Transition]
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x67 bytes
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x45 bytes
    System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallback(System.Windows.Forms.Control.ThreadMethodEntry tme) Line 7213 + 0xffffffc5 bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbacks() Line 7297 + 0xb bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Line 13848 C#
    System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) Line 1491 C#
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m) Line 1898 C#
    System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) Line 7515 C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14051 C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14106 C#
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg = 49512, System.IntPtr wparam, System.IntPtr lparam) Line 647 + 0xa bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m = {System.Windows.Forms.Message}) Line 814 + 0x1d bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 1409 C#
    Infragistics2.Win.UltraWinToolbars.v8.1.dll!Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.FormSubClasser.WndProcImpl(ref System.Windows.Forms.Message m) + 0x17f5 bytes
    Infragistics2.Win.UltraWinToolbars.v8.1.dll!Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.FormSubClasser.WndProc(ref System.Windows.Forms.Message m) + 0x5 bytes
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg = 49512, System.IntPtr wparam, System.IntPtr lparam) Line 647 + 0xa bytes C#
    [Native to Managed Transition]
    [Managed to Native Transition]
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = -1, int pvLoopData = 0) Line 2106 + 0x8 bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) Line 3377 + 0x1b bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3261 + 0xa bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run() Line 1457 C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleApp.LoadData() Line 318 + 0x5 bytes C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleApp.Run() Line 170 + 0x9 bytes C#
    Schedule.exe!ME.APTS.ScheduleApp.ScheduleApp.Main() Line 126 + 0xb bytes C#
    
    4 回复  |  直到 16 年前
        1
  •  9
  •   Patrick D'Souza ob1    13 年前

    大约一年前,我也遇到过同样的问题(应用程序挂起一段时间后没有用户交互,使用 OnUserPreferenceChanging()

    InvokeRequired / Invoke()

    解决办法是一直打电话 需要调用 / ISynchronizeInvoke 如果您不想在窗体类中引入依赖项)。

    here

        2
  •  14
  •   Hans Passant    10 年前

    是的,这是由SystemEvents类引起的一个相当臭名昭著的线程问题。我从来没有得到可靠的诊断,但90%的可能性是,这是由你的应用程序初始化问题触发的。

    根本问题是SystemEvents是由应用程序中的第一个窗体按需初始化的,该窗体具有对它生成的事件感兴趣的控件。如果第一种形式是 不 在主线程中创建,则SystemEvents无法猜测程序中哪个线程是UI线程。最终,当收到通知(比如UserPreferenceChanging)时,它会尝试在该线程上触发事件,但它已经不在了。SynchronizationContext类中的回退代码在线程池线程上引发事件。这不可避免地通过在没有创建窗口的线程上运行UI代码来调用线程地狱。当这种情况发生时,很多事情都会出错。死锁是工作站锁定后恢复桌面时特别常见的结果。

    is here . 是的,丑。理想情况下,控件知道如何处理此问题并封送通知本身。但是在.NET2.0、DataGridView、NumericUpDown、DomainUpDown、ToolStrip+MenuStrip和ToolStripItem派生的类不能做到这一点。我应该注意到RichTextBox和ProgressBar是可疑的,其余的都没问题。

        3
  •  2
  •   Luis Otero    11 年前

    我一直在遭受同样的问题,这总是由于Microsoft.Win32.SystemEvents.DisplaySettings更改事件在windows8.1下发生的频率更高,当我的应用程序正在运行并且有人连接到VNC或RDP时也会发生。在使用基于Mac的Fusion(VMWare)的Windows x.x时,这一点也非常清楚,它会不时地更改桌面设置。

    在尝试了很多事情之后,我终于通过在我的主应用程序(创建所有对话框并执行所有调用)中监听这些事件来解决问题

    声明:

    Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
    Microsoft.Win32.SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging;
    Microsoft.Win32.SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
    

    实施:

    static void SystemEvents_UserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
    {
        //Do nothing
    }
    
    static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
    {
        //Do nothing
    }
    
    static void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e)
    {
        //Do nothing
    }
    

    捕获这些事件没有任何作用,但这似乎是在消除当这些事件来自windows时我遇到的死锁,而我的代码的任何其他部分都在等待MainApp参与调用。

    希望这有帮助。

        4
  •  0
  •   Luis Otero    8 年前

    //Comment this line if you do not want visual styles and do not want to mess with SystemEvents.
    //Application.EnableVisualStyles();
    
        5
  •  0
  •   Vlad Rudenko    7 年前

    请检查密码 this 答案可以帮助您找到导致Windows窗体应用程序由于SystemEvents类的线程问题而冻结的确切事件处理程序。