簡體   English   中英

深入了解VB.NET事件

[英]VB.NET events handling in deep

任何人都知道事件在VB.NET中是如何工作的我特別懷疑的是,如果事件代碼未完成處理事件而發生新事件,將會發生什么。

IE:我有doubleClick事件,並且用戶反復多次堅持不懈地雙擊非常快。

事件代碼以相同的形式調用某些子例程和函數(非多線程)

事件代碼從頂部重新啟動還是等到完成后再執行所有代碼時重新輸入?

今天我遇到以下兩個錯誤

錯誤1:

Error al crear identificador de ventana.
en System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
en System.Windows.Forms.Control.CreateHandle()
en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
en System.Windows.Forms.Control.CreateControl()
en System.Windows.Forms.Control.ControlCollection.Add(Control value)
en System.Windows.Forms.TabControl.ControlCollection.Add(Control value)
en System.Windows.Forms.TabControl.TabPageCollection.Insert(Int32 index, TabPage tabPage)
en Commands.Form1.DataGridViewAlarms_CellMouseDoubleClick(Object sender, DataGridViewCellMouseEventArgs e)

錯誤2:

Error al crear identificador de ventana.
en System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
en System.Windows.Forms.Control.CreateHandle()
en System.Windows.Forms.Control.get_Handle()
en System.Windows.Forms.Control.CreateGraphicsInternal()
en System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
en System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t)
en System.Windows.Forms.Control.WndProcException(Exception e)
en System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e)
en System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
en System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
en System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
en System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
en System.Windows.Forms.Control.CreateHandle()
en System.Windows.Forms.TabControl.CreateHandle()
en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
en System.Windows.Forms.Control.CreateControl()
en System.Windows.Forms.Control.ControlCollection.Add(Control value)
en System.Windows.Forms.TabControl.ControlCollection.Add(Control value)
en System.Windows.Forms.TabControl.TabPageCollection.Insert(Int32 index, TabPage tabPage)
en Commands.Form1.DataGridViewAlarms_CellMouseDoubleClick(Object sender, DataGridViewCellMouseEventArgs e)

當我大約有20台具有相同程序的計算機時,不會總是發生此錯誤,只有一個用戶遇到此問題,而當他快速反復雙擊時,總是會發生此錯誤。


我使用以下代碼刪除TapPage

Dim CurrentTab As TabPage = AlarmsTabControl.SelectedTab
AlarmsTabControl.TabPages.Remove(CurrentTab)

WinForm框架是單線程的。 這樣,所有事件都在單線程上處理。 因此,當事件處理程序被調用時,直到第一個事件處理程序完成后,其他事件處理程序才會被調用。 換句話說,事件處理程序以串行方式調用。

事件處理程序可以被另一個事件中斷的唯一方法是,通過調用Application.DoEvents主動放棄控制。 可以想象,調用DoEvents會導致意外行為。 由於調用DoEvents通常會導致錯誤,因此不建議使用它,並且將其批評為不良設計的標志。 在這種情況下,通常表明使用BackgroundWorker更合適。

更深一點

當從操作系統接收到窗口消息時,將觸發與UI相關的事件。 傳入的窗口消息將添加到隊列中。 每個進程都有責任從隊列中讀取這些窗口消息並進行適當處理。 通常,這是在消息循環中完成的,該消息循環按順序處理隊列中的每個消息,或者閑置直到收到新消息。 每次消息循環找到要處理的新消息時,都會調用“ 窗口過程” 窗口過程通常稱為WindowProcWndProc 在.NET WinForm項目中,通過調用Application.Run啟動消息循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM