loop through the timer 5 times

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
I can check to see if the VPN is running in my application.

VB.NET:
 Public Function VPNRunning() As Boolean
        Try
            Dim pppAddress() As System.Net.NetworkInformation.NetworkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
            For Each cInterface As System.Net.NetworkInformation.NetworkInterface In pppAddress
                If cInterface.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.Ppp Then
                    TextBox1.Text = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString())

                    If pppIPAddresses = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString().Remove(10, 4)) Then
                        TextBox2.Text = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString().Remove(10, 4))
                        Return True
                    End If
                End If
            Next
            Return False
        Catch ex As Exception
        End Try
    End Function

Then on a button click I can verify this

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If VPNRunning() = True Then
           
        Else
            If MessageBox.Show("SU VPN is not running right now, Please Log into the VPN?.", "VPN not running", _
                   MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                ' Launch the Log In Dialog window
                Process.Start("rasphone.exe")
                Timer1.Start()
                Timer1_Timer()
            End If
        End If
    End Sub


If the VPN is not running I want to set a timer to check every 10 seconds to see if it is still running

VB.NET:
 Private Sub Timer1_Timer()
        'Code to Run every 10 secs
        If VPNRunning() = True Then
            ' do nothing
        Else
            Timer1.Enabled = True
            Timer1.Interval = 10000
            If MessageBox.Show("SU VPN is not running right now, Please Log into the VPN?.", "VPN not running", _
                   MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            End If
        End If
    End Sub

Yet it does not anything, can someone look at it and point out the error of my ways. I would like to loop through the timer 5 times and if the VPN is still not running close the application. I know that on

VB.NET:
me.close():D

Thanks
CoachBarker
 
The Timer Control needs to be started and stopped otherwise it will remain running as long as the application. It will do nothing until started. All that the timer does, for all intensive purposes, is raise an event "Tick" every xth of a second. You could therefore specify a global variable within your form which tracks the ammount of ticks. (intTicks) In the tick event handling code you would then increment intTicks and have a selection statement to do what you want based on the ammount of ticks.

Is this what you were looking for?

VB.NET:
Public Class Form1
Private intTicks as integer = 0
   Public Sub Form1_Load(byval sender as object, byval e as system.eventargs) Handles MyBase.Load
      Timer1.Start()
   End Sub
   Public Sub Timer1_Tick(byval sender as object, byval e as system.eventargs) Handles Timer1.Tick
      intTicks += 1

      If intTicks = 5 Then
         Me.Close()
      Else
        'run your code
      End if
   End Sub
End Class

I dont have the IDE right on me but that should work. But I would suggest just dragging the timer onto your form and double clicking it. Starting it when you want in your code. I would assume after the VPN check results in a false return.
 
This is what I have in a small sample form that does the basics of what I need it to do. When I run it the time code kicks right in without the buttton 1 being clicked. It shouldn't run unless the button has been clicked.

VB.NET:
Public Class Form1
    ' Declare a global variable that is the required IP Address to compare
    Dim pppIPAddresses As String = "128.230.90"
    Private intTicks As Integer = 0

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

    End Sub

    ' Method Name: VPNRunning()
    ' Purpose: To check and see if the VPN is currently running
    ' Parameters: non
    ' Change Log:
    '   8/8/2007:  Edited by Bill Barker
    Public Function VPNRunning() As Boolean
        Try
            Dim pppAddress() As System.Net.NetworkInformation.NetworkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
            For Each cInterface As System.Net.NetworkInformation.NetworkInterface In pppAddress
                If cInterface.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.Ppp Then
                    TextBox1.Text = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString())

                    If pppIPAddresses = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString().Remove(10, 4)) Then
                        TextBox2.Text = (cInterface.GetIPProperties.UnicastAddresses.Item(0).Address.ToString().Remove(10, 4))
                        Return True
                    End If
                End If
            Next
            Return False
        Catch ex As Exception
        End Try
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If VPNRunning() = False Then
            If MessageBox.Show("SU VPN is not running right now, Please Log into the VPN?.", "VPN not running", _
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                Process.Start("rasphone.exe")
                ' code here for the timer
            End If
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub
   
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        intTicks += 1

        If intTicks = 5 Then
            Me.Close()
        Else
            If VPNRunning() = False Then
                If MessageBox.Show("SU VPN is not running right now, Please Log into the VPN?.", "VPN not running", _
                                   MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                End If
            End If
        End If
    End Sub
End Class

Thank you for your assistance
CoachBarker
 
Make sure the timer's Enabled property is set to False in the designer.
When you want to start the timer (in the button's click event handler) call the Start method of the timer.
 
Back
Top