VB.NET - Timer Control - Asked By Ben Sebuabe on 28-Jun-13 09:32 AM

The code below loops through all the records and checks if Allocation_Due_Date is equal to the current date. If a match is found, the tooltip displays it on a button on the form. My problem is a time will come and the records will be many which will cause the machine to hang. So if someone can help me place the below code in a thread to free the machine from hanging. Thank you

' The sub procedure 'AllocationExpiryDate' is called to the Tick event of the timer control.

 Public Sub AllocationExpiryDate()
        Dim SQLCon As New SqlConnection : Dim dtTable As New DataSet
      strUsername = GetSetting("Allocation", "Connection", "DataL1")
        strPassword = GetSetting("Allocation", "Connection", "DataL2")
       strServerName = GetSetting("Allocation", "Connection", "DataL3")
       strInitialCatalog = GetSetting("Allocation", "Connection", "DataL4")
       DataL1 = strUsername & strPassword & strServerName & strInitialCatalog
        SQLCon.ConnectionString = DataL1
       Try
         SQLCon.Open()
         Dim Query As String = "Select * from Alloc order by Allocation_Due_Date asc"
        daAdapter = New SqlDataAdapter(Query, SQLCon)
       daAdapter.Fill(dtTable, "Alloc")
       Dim table As DataTable = dtTable.Tables("Alloc")
       If table.Rows.Count > 0 Then
         Dim ctr As Integer
          For ctr = 0 To table.Rows.Count - 1
            Dim dtAllocExpDate = FormatDateTime(table.Rows(ctr).Item("Allocation_Due_Date").ToString.Trim, DateFormat.ShortDate)
             If dtAllocExpDate = Now Then Then
               frmMainForm.ToolTip1.Show("Allocation note is due for revision. Refer to the allocation list", frmMainForm.btnExpiry)
             End If
          Next
        End If
     Catch ex As Exception
        MsgBox(Err.Description)
      End Try   
    End Sub
Robbe Morris replied to Ben Sebuabe on 29-Jun-13 08:12 PM
You need to create and set a flag while AllocationExpiryDate is actually running.  Set it at the start of the sub/method and turn it off in the Finally clause of your try/catch.

In the tick event handler, it should not call AllocationExpiryDate if the flag is ON.