0
votes

I have added a Timer control to a vb.net application that is used to check if a website is up or down. There are two Timer controls in the app. Both are from System.Windows.Forms.Timer.

There is Timer1 and Timer2. Timer1 is used to perform the website check. Timer2 is simply to display a current date and time in the ToolStrip at the base of the form. The Timer2 runs without issue displaying the time, but when I start the website check that runs Timer1, the application freezes after two checks. Sometimes it runs longer, but eventually freezes.

I have to End Task from within Task Manager in order to shut down the application. I have tested this from within the Debugger and after running the executable. I have also completely removed Timer2 and tested, but the freezing remains. Here is my code. Any assistance would be greatly appreciated.

Imports System


Public Class Form1

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

        lblStatus.Text = ""
        Timer1.Stop()

        btnStart.Enabled = True
        btnStop.Enabled = False

    End Sub

    Public Function CheckAddress(ByVal URL As String) As Boolean
        Try
            Dim request As WebRequest = WebRequest.Create(URL)
            Dim response As WebResponse = request.GetResponse()
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        Dim startPoint As Integer = -1

        If CheckAddress(txtAddress.Text).ToString() = True Then
            rtxtResults.AppendText(" -- " & txtAddress.Text & " - Website shows UP at " & tsClock.Text & " - " & vbNewLine)
        ElseIf CheckAddress(txtAddress.Text).ToString() = False Then
            rtxtResults.AppendText(" -- " & txtAddress.Text & " - Website shows DOWN at " & tsClock.Text & " - " & vbNewLine)
        End If

        Do
            startPoint = rtxtResults.Find("Website shows DOWN at", startPoint + 1, RichTextBoxFinds.None)
            If (startPoint >= 0) Then
                rtxtResults.SelectionStart = startPoint
                rtxtResults.SelectionLength = "Website shows DOWN at".Length
                rtxtResults.SelectionColor = Color.Red
            End If
        Loop Until startPoint < 0


    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs)
        tsClock.Text = Now()
    End Sub

    Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

        If txtInterval.Text = "" Then
            MsgBox("Must enter a number")
            Exit Sub
        End If
        lblStatus.ForeColor = Color.Green
        lblStatus.Text = "Running"
        btnStart.Enabled = False
        btnStop.Enabled = True
        Timer1.Interval = Int(txtInterval.Text) * 1000
        Timer1.Start()

    End Sub

    Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
        lblStatus.ForeColor = Color.Red
        lblStatus.Text = "Stopped"
        btnStop.Enabled = False
        btnStart.Enabled = True

        Timer1.Stop()
    End Sub


End Class
1
The Timer is not a control. To be a control, a class must inherit, either directly or indirectly, the System.Windows.Forms.Control class. That Timer class does not. It is a component, because it implements the IComponent interface, which is all that's required to be used in the designer. Controls are a special type of component. - jmcilhinney
Calling all background threads! - Ally

1 Answers

2
votes

Your application's UI will "freeze" until the Timer1_Tick method completes. Most likely your Timer1_Tick's Do...Loop is not exiting. Try deleting the Do and Loop lines.