0

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
2
  • 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. Commented Mar 22, 2017 at 1:01
  • 1
    Calling all background threads! Commented Mar 22, 2017 at 2:34

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Rather than deleting the loop, it would be better to actually debug the code to see WHY the loop never exits.
I eliminated the entire Do...Loop. The application still "freezes".
@J.Yonan - I don't believe that would be the case. Can you post the updated code that still "freezes"? (And why did you put "freezes" in double quotes?)
@Enigmativity - A previous responder framed my use of the word Freezes in quotes. Regardless, I commented out the entire Do...Loop and the Dim statement within the Timer1_Tick Sub. The application still stopped after two responses.
@J.Yonan - Is it freezing because request.GetResponse() is taking too long?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.