2011-11-29 40 views
2

我的WinForm應用程序有一個掛起問題。發生什麼事情是客戶端有時會讓應用程序在一夜之間運行,並且在早上應用程序回滾時通常處於掛起狀態。這是我在主線程中的轉儲文件中看到的。我不明白的是什麼可以使SystemEvents.OnUserPreferenceChanged事件被調用,雖然我不認爲我正在做任何調用此事件的事情。如何處理這個C​​#掛起系統SystemEvents.OnUserPreferenceChanged

0024e480 770496f4 System.Threading.WaitHandle.WaitOneNative(Microsoft.Win32.SafeHandles.SafeWaitHandle, UInt32, Boolean, Boolean) 
0024e52c 702c68af System.Threading.WaitHandle.WaitOne(Int64, Boolean) 
0024e548 702c6865 System.Threading.WaitHandle.WaitOne(Int32, Boolean) 
0024e55c 6e891a6f System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle) 
0024e570 6ebcd6eb System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean) 
0024e610 6e8933cc System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[]) 
0024e644 6eac0c83 System.Windows.Forms.WindowsFormsSynchronizationContext.Send(System.Threading.SendOrPostCallback, System.Object) 
0024e65c 6fe1eed2 Microsoft.Win32.SystemEvents+SystemEventInvokeInfo.Invoke(Boolean, System.Object[]) 
0024e690 6fe1d07f Microsoft.Win32.SystemEvents.RaiseEvent(Boolean, System.Object, System.Object[]) 
0024e6dc 6fe1e38f Microsoft.Win32.SystemEvents.OnUserPreferenceChanged(Int32, IntPtr, IntPtr) 
0024e6fc 6fa64c29 Microsoft.Win32.SystemEvents.WindowProc(IntPtr, Int32, IntPtr, IntPtr) 
0024e700 000a1104 [InlinedCallFrame: 0024e700] 
0024e8d8 6e378d5e System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32, Int32, Int32) 
0024e974 6e3789c7 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext) 
0024e9c8 6e378811 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext) 
0024e9f8 6e88de47 System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form) 
0024ea0c 6e8c25cb System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window) 
0024ea98 6e8c27e3 System.Windows.Forms.Form.ShowDialog() 
0024ea9c 56c26e76 MyNameSpace.MyForm.MyMethod2(Object, Boolean, Boolean, System.Guid, Boolean) 
0024eba0 56c26c47 MyNameSpace.MyForm.MyMethod1(System.Guid, System.Guid, System.Guid, Boolean) 
0024ecf8 56c91f4c MyNameSpace.MyForm.MyButton_Click(System.Object, System.EventArgs) 
0024ee88 6e334180 System.Windows.Forms.Control.OnClick(System.EventArgs) 

回答

4

控件訂閱此事件,以便在用戶更改主題或系統顏色時重新繪製自己。當您不靠近機器時,該事件也會被觸發,Windows會自動鎖定工作站。這解釋了宿醉後的早晨和之後。

死鎖是由線程問題引起的,SystemEvents類在錯誤的線程上觸發事件。這是由程序中的初始化問題引起的。典型的觸發器不是在主線程上創建第一個窗口,這會混淆SystemEvent。它試圖再次在同一個線程上觸發一個事件,但它不在身邊。或者它在Winforms初始化之前複製SynchronizationContext.Current。無論哪種方式,事件將在線程池線程而不是主UI線程上觸發。這是致命的。

例如,當您實現自己的啓動畫面時很常見。改爲使用built-in support

+0

感謝@Hans的回覆。是的,我的表單中有一個啓動畫面。這是否意味着我應該在該窗體被棄置之前從該初始屏幕窗體中取消訂閱SystemEvent? –

+0

不,取消訂閱不會解決問題,您也不能解決問題。這是引發問題的訂閱。 –