VB.NET - Background worker - Asked By Ben Sebuabe on 29-Jan-14 12:08 PM

I am using vb.net 2008 which is connected to sql 2008. I have a timer control and a tooltip. The timer always keeps on ticking and when a condition is met the tooltip displays a message. My problem now is the timer hangs or freezes the program.
I thought of a Background worker that works on a separate thread, but how to go about it.
How can I put the IterateMovement procedure below in a background worker? Thanks

Private Sub TimerIterate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerIterate.Tick
      Call IterateMovement()
    End Sub


Public Sub IterateMovement()
    Dim SQLCon As SqlConnection
    Dim dtDays As New DataSet
    strUsername = GetSetting("Lands", "Connection", "DataL1")
    strPassword = GetSetting("Lands", "Connection", "DataL2")
    strServerName = GetSetting("Lands", "Connection", "DataL3")
    strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
    DataL1 = strUsername & strPassword & " Connect Timeout=0; " & strServerName & strInitialCatalog
    SQLCon = New SqlConnection
    daAdapter = New SqlDataAdapter
    dtDays = New DataSet
    SQLCon.ConnectionString = DataL1
    'Weekly Files to be received
    Dim Query As String = "Select * from File_Movement where Movement_to ='" & frmVisibleNot.txtUsername.Text.Trim & "' and Received_By is null and fdate >  dateadd(day,-7, getdate()) order by Fdate desc,Ftime desc"
    daAdapter.SelectCommand = New SqlCommand(Query, SQLCon)
    Try
      SQLCon.Open()
      daAdapter.Fill(dtDays, "File_movement").ToString.Trim()
      Dim table As DataTable = dtDays.Tables("File_movement")
      If table.Rows.Count > 0 Then
        frmHome.ToolTip1.Show("YOU HAVE FILE(S) TO RECEIVE, CLICK ON THE BUTTON TO SEE THEM", frmHome.btnMessage, 10000)
        frmHome.ToolTip1.ToolTipTitle = "Lands Messenger"
        frmHome.ToolTip1.UseAnimation = True
        frmHome.btnMessage.Visible = True
        frmHome.ToolTip1.BackColor = Color.Red
        frmHome.ToolTip1.ForeColor = Color.White
      Else
        frmHome.ToolTip1.Hide(frmHome.btnMessage)
        frmHome.btnMessage.Visible = False
      End If  
      daAdapter.Dispose()
      dtDays.Dispose()
      SQLCon.Dispose()
      daAdapter = Nothing
      SQLCon.Close()
      SQLCon = Nothing
    Catch
   
    End Try
  End Sub
#End Region
Robbe Morris replied to Ben Sebuabe on 29-Jan-14 12:46 PM
In your case, I don't think it needs to be that complex.  Create a class level bool variable called _inProgress

At the beginning of IterateMovement, set this variable to true.  In the finally clause of IterateMovement (you'll need to add that after your Catch statement), set _inProgress = false

In the TimerIterate_Tick event, check to see if _inProgress = true.  If so, don't call IterateMovement and exit the sub.

My guess is that your stuff is getting hung up because you are setting the Timer to tick every few milliseconds and then calling IterateMovement again and again before it had a chance to complete.  This simple arrangement will let you constantly check every few milliseconds but not execute again until the previous IterateMovement has finished.