We help IT Professionals succeed at work.
Get Started

Multithreading using backgroundworker

330 Views
Last Modified: 2013-11-26
I have the following code under a button_click event on my form. In the code at the looping block the UI freezes. So i would like to use multi threading so that my UI doesnt freeze. I have no idea where to start and how to start implementing the background worker. I have tried reading numerous tutorials online but im unable to get the overall idea.

Could anyone please gimme a walkthrough on how it is done...
Conn.ConnectionString = conns
        Dim Cmd As New SqlDataAdapter
        Dim cmd1 As New SqlDataAdapter
        Conn.Open()
 
        Cmd = New SqlDataAdapter("Select * from mast_de_sellout where calc_mon = '" & months.SelectedValue & "' and tdp = '" & half2.Text & "'", Conn)
        Dim stable As New DataTable
        Dim ltable As New DataTable
        Cmd.Fill(stable)
        Dim selected As Integer
        Dim query As String
        selected = half2.SelectedValue
        If selected = 1 Then
 
            query = "Select * from mast_de_sellout where calc_mon != '" & months.SelectedValue & "'  "
 
        ElseIf selected = 2 Then
 
            query = "Select * from mast_de_sellout where NOT(calc_mon = '" & monthsp.SelectedValue & "' and tdp = 'H2')  "
 
        ElseIf selected = 3 Or half.SelectedValue = 4 Then
 
            query = "Select * from mast_de_sellout where calc_mon != '" & months.SelectedValue & "'  "
 
        Else
 
            query = "Select * from mast_de_sellout where calc_mon != '" & months.SelectedValue & "' "
 
        End If
        cmd1 = New SqlDataAdapter(query, Conn)
        cmd1.Fill(ltable)
        Dim command As New SqlCommand
 
      
        Dim builder As New SqlCommandBuilder(Cmd)
        Dim builder2 As New SqlCommandBuilder(cmd1)
        Dim k As Integer
        Dim i As Integer
        Dim j As Integer
        For k = 0 To stable.Rows.Count - 1
            stable.Rows(k).Item("IMEI_Dup_status_3") = 1
        Next
        ProgressBar3.Minimum = 0
        ProgressBar3.Maximum = ltable.Rows.Count + 20
        For i = 0 To ltable.Rows.Count - 1
            ProgressBar3.Value = ProgressBar3.Value + 1
            For j = 0 To stable.Rows.Count - 1
                If ltable.Rows(i).Item("imei") = stable.Rows(j).Item("imei") Then
                    If ltable.Rows(i).Item("salesman_id") = stable(j).Item("salesman_id") Then
                        If ltable.Rows(i).Item("dealer_id") = stable.Rows(j).Item("dealer_id") Then
                            If ltable.Rows(i).Item("transaction_type") = "PAY_TRANSACTION" Or ltable.Rows(i).Item("transaction_type") = "ALREADY_PAID_TRANSACTION" Then
                                stable.Rows(j).Item("IMEI_dup_status_3") = 0
                            End If
                        End If
                    End If
                End If
            Next
        Next
      
        ProgressBar3.Value = 0
        ProgressBar3.Refresh()
        ProgressBar3.Minimum = 0
        ProgressBar3.Maximum = stable.Rows.Count + 20
        For i = 0 To stable.Rows.Count - 1
            ProgressBar3.Value += 1
            command = Conn.CreateCommand()
            command.CommandText = "UPDATE mast_de_sellout SET IMEI_dup_status_3 = '" & stable.Rows(i).Item("IMEI_dup_status_3") & "' where sellout_id = '" & stable.Rows(i).Item("sellout_id") & "' and tdp = '" & half2.Text & "' "
            command.ExecuteNonQuery()
 
        Next
        ltable.Dispose()
        stable.Dispose()
        MsgBox("validation Completed Successfully")
       
        Conn.Close()
        ProgressBar3.Value = 0

Open in new window

Comment
Watch Question
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE