Looping an application

watcher2007

Member
Joined
Apr 7, 2008
Messages
7
Programming Experience
5-10
Hi,

I have an application that polls a DB for new records. Since this a periodic check, I've put the code in a "While" loop, which never terminates.

The application runs fine, however, when I deploy the application, the only way that I can terminate the application, once it is running, is by terminating it via "Task Manager". how can I inlcude a stop button, on the application form, and make it active, even though the loop is still running.

Thanks in advance.
 
Use a Do While loop and include an exit loop variable (boolean) and use the FormClosing event to set this exit variable to True and that loop will exit and allow the app to close gracefully
 
VB.NET:
Private m_ExitLoopBoolean As Boolean = False

Do While <your current conditions> AndAlso m_ExitLoopBoolean = False
  'Loop code here
Loop

'In the FormClosing event:
        Select Case e.CloseReason
            Case CloseReason.ApplicationExitCall, CloseReason.TaskManagerClosing, CloseReason.WindowsShutDown, CloseReason.FormOwnerClosing
                m_ExitLoopBoolean = True
        End Select
This of course is based on your loop being on a separate thread (BackgroundWorker)
 
Hi,

Thanks again for the help thus far.....

I'm not sure what you mean by background worker. my current code is as follows "cmdCreate_Click" is a button, that I click to start the loop;

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click

Dim CONT As Integer = 0
While CONT <> 1
'Loop code is here
End While

End Sub

How would I encorporate your code in this scenario?

Thanks in advance....
 
Easily:
VB.NET:
Private m_ExitLoopBoolean As Boolean = False

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
  Dim CONT As Integer = 0

  Do While CONT <> 1 AndAlso m_ExitLoopBoolean = False
    'Loop code here
  Loop
End Sub

Private Sub frmQuickAddBuddy_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Select Case e.CloseReason
        Case CloseReason.ApplicationExitCall, CloseReason.TaskManagerClosing, CloseReason.WindowsShutDown, CloseReason.FormOwnerClosing
            m_ExitLoopBoolean = True
        Case Else
            e.Cancel = True
    End Select
End Sub

And please don't leave people visitor's notes requesting help, that's what these threads are for.
 
Hi,

Many thanks for that.....

Not to be a bother, but if I wanted to include "Stop" button on the form to terminate the loop, how would I go about doing it??

Thanks again....
 
in you're stop button put this 'm_ExitLoopBoolean = True'

And I would highly suggest putting that loop on a separate thread too, look into the BackgroundWorker control. It's in your toolbox by default and there are lots of examples on how to use it available
 
Back
Top