I was experimenting with DoEvents and have an oddity... I'm having trouble having it acknowledge the Form_Closing() Event properly.
In it's simplest incarnation, I tried this:
Now this didn't do so well, LOL. When the btnStart was clicked, the use of 'Application.DoEvents' did allow the Form to exit, but:
(1) I had to click the Red Close <X> twice to get it to acknowledge.
(2) Although the Form did in fact disappear, it was still "running", which I could tell because the Immediate window in the debugger was still open.
The fact that it was still "running" although "gone" is interesting, this could not occur in VB6, but I guess as long as the Code is "alive" (that is, still running), then I guess it is still on the stack and the Object/Form (or at least some part of it) still exists?
My next idea was to do two things:
(1) Use Mod 1000 so that I am not flooding so many DoEvents calls. I didn't expect anything from this, although it should speed up the loop since DoEvents is pretty heavy.
(2) I added a 'Private m_Shutdown As Boolean' to trigger the exit of the Loop.
The results of the above were much better... but not perfect:
(1) It did now close cleanly. That is, when the Form Closed, the Loop was no longer running. Perfect.
(2) However, it does still require that the Form's Red Close <X> be clicked twice.
Any ideas on why the <X> needs to be hit twice? (See Attached VB.Net Project.)
Hmmm... ok, wait, if you run the project, hit the <Start> button on the Form and then hit the <X> button, the interesting thing is that on the first click, the <Start> button will depress. This seems to occur no matter where you click on the Form or Titlebar as well.
So is this a sort of "TakeFocusOnClick" sort of situation? There must be a simple way to allow for the first click to actually go where the User wants, no? I'm just too n00b in .Net to quite understand what is happening here.
Thanks in advance for all those interested/help out...
In it's simplest incarnation, I tried this:
Visual Basic:
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnStart.Click
Do
Application.DoEvents()
Loop
End Sub
(1) I had to click the Red Close <X> twice to get it to acknowledge.
(2) Although the Form did in fact disappear, it was still "running", which I could tell because the Immediate window in the debugger was still open.
The fact that it was still "running" although "gone" is interesting, this could not occur in VB6, but I guess as long as the Code is "alive" (that is, still running), then I guess it is still on the stack and the Object/Form (or at least some part of it) still exists?
My next idea was to do two things:
(1) Use Mod 1000 so that I am not flooding so many DoEvents calls. I didn't expect anything from this, although it should speed up the loop since DoEvents is pretty heavy.
(2) I added a 'Private m_Shutdown As Boolean' to trigger the exit of the Loop.
Visual Basic:
Private m_Shutdown As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnStart.Click
Dim count As Long
Do
count += 1
If (count Mod 1000) = 0 Then
Application.DoEvents()
End If
Loop Until m_Shutdown
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
MessageBox.Show ("Form1_Closing()")
m_Shutdown = True
End Sub
Private Sub Form1_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Closed
MessageBox.Show ("Form1_Closed()")
End Sub
(1) It did now close cleanly. That is, when the Form Closed, the Loop was no longer running. Perfect.
(2) However, it does still require that the Form's Red Close <X> be clicked twice.
Any ideas on why the <X> needs to be hit twice? (See Attached VB.Net Project.)
Hmmm... ok, wait, if you run the project, hit the <Start> button on the Form and then hit the <X> button, the interesting thing is that on the first click, the <Start> button will depress. This seems to occur no matter where you click on the Form or Titlebar as well.
So is this a sort of "TakeFocusOnClick" sort of situation? There must be a simple way to allow for the first click to actually go where the User wants, no? I'm just too n00b in .Net to quite understand what is happening here.
Thanks in advance for all those interested/help out...
Attachments
Last edited by a moderator: