• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Exit an application from a do...loop

Hi Experts,

The attached code will execute a do...loop for 30 seconds and exit. If the form's close (red x) is clicked the application does not exit the loop. How should this code be rewritten to correctly handle the close request.
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Show()

        MySub()

    End Sub

    Private Sub MySub()

        Dim OverTime As DateTime = Now.AddSeconds(30)

        Do Until Now > OverTime

            Threading.Thread.Sleep(100)
            Application.DoEvents()

        Loop

    End Sub

End Class

Open in new window

0
DColin
Asked:
DColin
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you should not use such a loop to "wait", but a simple timer instead...
0
 
DColinAuthor Commented:
angelIII:
you should not use such a loop to "wait", but a simple timer instead...

Any chance of showing me how?
0
 
CodeCruiserCommented:
Try increasing the wait amount. Or else, subtract the now from overtime and set that timespan as the timer for a timer object. Drag and drop a timer on the form. Double click it to code for the tick event. Then calculate the timespan in the code and set the time like below.

        Dim overtime As DateTime = Now.AddSeconds(30)
        Dim t As TimeSpan = overtime - Now
        timer1.Interval = t.Milliseconds
        timer1.enabled = True
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also toggle a flag when the FormClosing event fires.  Add a check to the flag as part of the loop:
Public Class Form1

    Private FormIsClosing As Boolean = False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        MySub()
    End Sub

    Private Sub MySub()
        Dim OverTime As DateTime = Now.AddSeconds(30)
        Do Until Now > OverTime OrElse FormIsClosing
            Threading.Thread.Sleep(100)
            Application.DoEvents()
        Loop
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        FormIsClosing = True
    End Sub

End Class

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree with angelIII's though...it's NOT a good design.  The concept can be applied to other situations though...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now