By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,337 Members | 2,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,337 IT Pros & Developers. It's quick & easy.

Using Background worker

P: 54
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub TimerIterate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerIterate.Tick
  2.         Call IterateMovement()
  3.     End Sub
  4.  
  5.  
  6. Public Sub IterateMovement()
  7.         Dim SQLCon As SqlConnection
  8.         Dim dtDays As New DataSet
  9.         strUsername = GetSetting("Lands", "Connection", "DataL1")
  10.         strPassword = GetSetting("Lands", "Connection", "DataL2")
  11.         strServerName = GetSetting("Lands", "Connection", "DataL3")
  12.         strInitialCatalog = GetSetting("Lands", "Connection", "DataL4")
  13.         DataL1 = strUsername & strPassword & " Connect Timeout=0; " & strServerName & strInitialCatalog
  14.         SQLCon = New SqlConnection
  15.         daAdapter = New SqlDataAdapter
  16.         dtDays = New DataSet
  17.         SQLCon.ConnectionString = DataL1
  18.         'Weekly Files to be received
  19.         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"
  20.         daAdapter.SelectCommand = New SqlCommand(Query, SQLCon)
  21.         Try
  22.             SQLCon.Open()
  23.             daAdapter.Fill(dtDays, "File_movement").ToString.Trim()
  24.             Dim table As DataTable = dtDays.Tables("File_movement")
  25.             If table.Rows.Count > 0 Then
  26.                 frmHome.ToolTip1.Show("YOU HAVE FILE(S) TO RECEIVE, CLICK ON THE BUTTON TO SEE THEM", frmHome.btnMessage, 10000)
  27.                 frmHome.ToolTip1.ToolTipTitle = "Lands Messenger"
  28.                 frmHome.ToolTip1.UseAnimation = True
  29.                 frmHome.btnMessage.Visible = True
  30.                 frmHome.ToolTip1.BackColor = Color.Red
  31.                 frmHome.ToolTip1.ForeColor = Color.White
  32.             Else
  33.                 frmHome.ToolTip1.Hide(frmHome.btnMessage)
  34.                 frmHome.btnMessage.Visible = False
  35.             End If    
  36.             daAdapter.Dispose()
  37.             dtDays.Dispose()
  38.             SQLCon.Dispose()
  39.             daAdapter = Nothing
  40.             SQLCon.Close()
  41.             SQLCon = Nothing
  42.         Catch
  43.  
  44.         End Try
  45.     End Sub
  46. #End Region
  47.  
Jan 29 '14 #1
Share this Question
Share on Google+
1 Reply


100+
P: 299
I found this website very useful when learning about BackgroundWorkers. http://www.vbforums.com/showthread.p...rker-Component
Jan 29 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.