我正在處理基於.Net 2.0/C#的大型WinForm應用程序,在運行應用程序時出現穩定的可重現錯誤「創建窗口句柄時出錯」筆記本電腦戴爾E6520(Win7專業版SP1,8G內存),雖然它適用於臺式機,包括XP,Win7,2008)。異常「創建窗口句柄時出現錯誤」碰巧在工具條上的組合框
這裏的異常堆棧:
System.ComponentModel.Win32Exception: Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.ComboBox.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.WindowsFormsUtils.ReadOnlyControlCollection.AddInternal(Control value)
at System.Windows.Forms.ToolStripControlHost.SyncControlParent()
at System.Windows.Forms.ToolStripControlHost.OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
at System.Windows.Forms.ToolStripItem.set_ParentInternal(ToolStrip value)
at System.Windows.Forms.ToolStripSplitStackLayout.LayoutHorizontal()
at System.Windows.Forms.ToolStripSplitStackLayout.LayoutCore(IArrangedElement container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ToolStrip.OnLayout(LayoutEventArgs e)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
時被示出的形式,其中內有含組合框和按鈕的工具條它發生。他們是後來加入的形式構造後動態工具條,同時保持看不見,通過調用下面的方法:內初始化
private void Init()
{
...
foreach (ToolBarItemConfig item in configuredToolbarItems)
{
ToolStripItem toolStripItem = createToolStripItem(item);
toolStrip.Items.Add(toolStripItem);
if (toolStripItem is ToolStripComboBox)
populateComboBox(toolStripItem as ToolStripComboBox, item); // populate items of ToolStripComboBox
}
...
}
注意,該工具條是不可見的,新的ToolStripComboBox控件項目處理不創建(從日誌中找到)。我可以理解句柄創建是由意圖推遲的,因爲它們實際上還沒有顯示出來。然而,在稍後的時間,表格終於顯示出來,並且出現異常。
我從this article得到了很多靈感,並且可以通過在ToolStripComboBox項創建時立即訪問Handle屬性來強制創建句柄來解決此問題。
現在,我的理解是ToolStrip的嘗試中所示的形式時,重新創建其OnLayout組合框的手柄,但當時這些句柄仍尚未創建,因此沒有摧毀 - >創建時談到「錯誤窗口句柄「。然而,這只是我的猜測,因爲問題的根源仍然不清楚,特別是我不能解釋爲什麼這隻發生在特定的筆記本電腦,而不是在臺式機。
任何人都可以幫助我理解它的根本原因,所以我可以確信類似的問題將來不會再受到傷害嗎?
在此先感謝。
感謝您的回覆。事實上,這是我們第一次遇到問題時的第一個懷疑,但在後來的觀察中,這可能會被排除,因爲用戶對象和句柄的數量遠低於極限(每個過程10k)大約爲1.2k。 – William