Urgent and Interesting!!! EventHandling Bug!!!????
-
I have an object that fires an event and a form that handles that event. When handling the event i show a new form but the new form hangs. It gets "Loaded", "VisibleChanged" and "Activated" events and hangs. I am attaching vb code as an example. Please see and if you can figure out why it is so i will really appreciate if you tell me. Thanks in advance!!!! P.S. as I understand its a bug - the form must show without problems!!! here is the sample code of the error in vb.net:))) 'Class EventRaiser raises SomeEvent 3 seconds after it is loaded. 'EventHandlerForm handles that event and shows modeless form. 'Normally the new form must have no problems but actually it 'receives "Load", "VisibleChanged" and "Activated" events and hangs. 'Raises event "SomeEvent" 3 seconds after it is created. Public Class EventRaiser Public Event SomeEvent() Private WithEvents myTimer As New System.Timers.Timer() Sub New() myTimer.Interval = 3000 myTimer.Start() End Sub Private Sub mytimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles myTimer.Elapsed RaiseEvent SomeEvent() myTimer.Enabled = False End Sub End Class Public Class EventHandlerForm Inherits System.Windows.Forms.Form 'EventRaiser raises SomeEvent 3 seconds after it is created. Private WithEvents myEventRaiser As New EventRaiser() #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label() Me.Suspend
-
I have an object that fires an event and a form that handles that event. When handling the event i show a new form but the new form hangs. It gets "Loaded", "VisibleChanged" and "Activated" events and hangs. I am attaching vb code as an example. Please see and if you can figure out why it is so i will really appreciate if you tell me. Thanks in advance!!!! P.S. as I understand its a bug - the form must show without problems!!! here is the sample code of the error in vb.net:))) 'Class EventRaiser raises SomeEvent 3 seconds after it is loaded. 'EventHandlerForm handles that event and shows modeless form. 'Normally the new form must have no problems but actually it 'receives "Load", "VisibleChanged" and "Activated" events and hangs. 'Raises event "SomeEvent" 3 seconds after it is created. Public Class EventRaiser Public Event SomeEvent() Private WithEvents myTimer As New System.Timers.Timer() Sub New() myTimer.Interval = 3000 myTimer.Start() End Sub Private Sub mytimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles myTimer.Elapsed RaiseEvent SomeEvent() myTimer.Enabled = False End Sub End Class Public Class EventHandlerForm Inherits System.Windows.Forms.Form 'EventRaiser raises SomeEvent 3 seconds after it is created. Private WithEvents myEventRaiser As New EventRaiser() #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label() Me.Suspend
There is something about creating the new form inside that event handler. Creating the few form inside the handler for a button works perfectly, but not inside the myEventRaiser.SomeEvent handler for some reason. RageInTheMachine9532
-
I have an object that fires an event and a form that handles that event. When handling the event i show a new form but the new form hangs. It gets "Loaded", "VisibleChanged" and "Activated" events and hangs. I am attaching vb code as an example. Please see and if you can figure out why it is so i will really appreciate if you tell me. Thanks in advance!!!! P.S. as I understand its a bug - the form must show without problems!!! here is the sample code of the error in vb.net:))) 'Class EventRaiser raises SomeEvent 3 seconds after it is loaded. 'EventHandlerForm handles that event and shows modeless form. 'Normally the new form must have no problems but actually it 'receives "Load", "VisibleChanged" and "Activated" events and hangs. 'Raises event "SomeEvent" 3 seconds after it is created. Public Class EventRaiser Public Event SomeEvent() Private WithEvents myTimer As New System.Timers.Timer() Sub New() myTimer.Interval = 3000 myTimer.Start() End Sub Private Sub mytimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles myTimer.Elapsed RaiseEvent SomeEvent() myTimer.Enabled = False End Sub End Class Public Class EventHandlerForm Inherits System.Windows.Forms.Form 'EventRaiser raises SomeEvent 3 seconds after it is created. Private WithEvents myEventRaiser As New EventRaiser() #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label() Me.Suspend
You "Dim"-ed the Form variable inside the Event Handler. Remember that anytime a Routine goes out of Scope, the Variables in that Routine also get destroyed. This could be causing the problem. Dim the Form variable outside of the Event Handler routine. Hope that helps!
-
You "Dim"-ed the Form variable inside the Event Handler. Remember that anytime a Routine goes out of Scope, the Variables in that Routine also get destroyed. This could be causing the problem. Dim the Form variable outside of the Event Handler routine. Hope that helps!
-
I've tried that already. But in the example i use System.Timers.Timer class. If i use the System.Windows.Forms.Timer then it works ok. I want to understand whats the difference in the events???
Ahh, Ok, then this might be because the System.Timers work on a separate Thread. The Windows.Forms.Timer works because it's on the same thread as the UI. In your Code you need to say "Me.Invoke(..." in order to get your new Form loaded into the MAIN UI Thread. Other wise, it's loading your new Form into a separate Thread in which you cannot control this new Form. Read up on Multithreading...a good example is the Chat application in the MSDN.
-
Ahh, Ok, then this might be because the System.Timers work on a separate Thread. The Windows.Forms.Timer works because it's on the same thread as the UI. In your Code you need to say "Me.Invoke(..." in order to get your new Form loaded into the MAIN UI Thread. Other wise, it's loading your new Form into a separate Thread in which you cannot control this new Form. Read up on Multithreading...a good example is the Chat application in the MSDN.