Using Background worker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benniit
    New Member
    • May 2008
    • 54

    #1

    Using Background worker

    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 so that every 5 seconds it will check for the condition? Thanks

    Code:
    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
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    I found this website very useful when learning about BackgroundWorke rs. http://www.vbforums.com/showthread.p...rker-Component

    Comment

    Working...