gpt4 book ai didi

c# - Windows 窗体应用程序在夜间运行时随机卡住

转载 作者:行者123 更新时间:2023-12-02 08:44:54 27 4
gpt4 key购买 nike

我有一个窗口窗体应用程序,它有多个正在运行的线程,这些线程将调用主 UI 线程来更新 UI。有时,在开发机器上,应用程序主 UI 线程将停止运行,并且应用程序不再响应。如果我让应用程序运行过夜,似乎就会发生这种情况。但是,我有一些用户通过远程桌面运行此窗口窗体应用程序,如果该应用程序在没有用户交互的情况下整夜运行,则此问题会更频繁地发生。

我找到了一个article似乎正在描述这个问题,但我没有足够的Windows开发知识来弄清楚为什么应用程序会卡住。

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

这个问题已经困扰我很长一段时间了。如果有任何建议或意见,我将不胜感激。

谢谢!

Main UI thread stack trace:mscorlib.dll!System.Threading.WaitHandle.WaitOne(long timeout, bool exitContext) + 0x2f bytesmscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) + 0x25 bytesSystem.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 bytesSystem.dll!Microsoft.Win32.SystemEvents.RaiseEvent(bool checkFinalization = true, object key = {object}, object[] args = {object[2]}) + 0x10f bytesSystem.dll!Microsoft.Win32.SystemEvents.OnUserPreferenceChanging(int msg, System.IntPtr wParam, System.IntPtr lParam) + 0x77 bytesSystem.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 bytesSystem.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 bytesmscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x45 bytesSystem.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 bytesInfragistics2.Win.UltraWinToolbars.v8.1.dll!Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.FormSubClasser.WndProc(ref System.Windows.Forms.Message m) + 0x5 bytesSystem.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#

最佳答案

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

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

这不是唯一可能出错的方式,如果您在另一个线程上创建任何表单,这是不可避免的。现在 SystemEvents 不可能在正确的线程上引发事件,当然有人会失败。演示调试技术的博客文章 is here 。是的,丑陋。理想情况下,控件知道如何处理此问题并整理通知本身。但这在 .NET 2.0 中被遗忘了,DataGridView、NumericUpDown、DomainUpDown、ToolStrip+MenuStrip 和 ToolStripItem 派生类不会执行此操作。我应该注意到 RichTextBox 和 ProgressBar 是可疑的,其余的都还好。

检查应用程序的启动顺序。创建自己的启动屏幕是一个很好的方法,请使用 WindowsFormsApplicationBase 类提供的内置支持。如果你自己做,那就保持非常简单,只是一个位图。如前所述,任何可能在工作线程上创建自己的表单的地方都会带来麻烦。始终以相反的方式进行,在工作线程上运行昂贵的代码并将 UI 保留在主线程上。

关于c# - Windows 窗体应用程序在夜间运行时随机卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3832156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com