Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi am new with Threading and I want to Upgrade my Project so that when ever I Query to my Database "MySql" And "MS Sql" my UI won't hang up

I've Tried Application.DoEvent but still I want to try Threading it gives me challenge to myself

:)

so here's my Code

VB
ListView Click Event

Dim LoadQuery As New Thread(AddressOf LoadToAnotherListView)
LoadQuery.IsBackground = True
LoadQuery.Start()
CheckForIllegalCrossThreadCalls = False


VB
Public Sub LoadToAnotherListView
Dim count As Integer = 0
Using con As New SqlClient.SqlConnection(ConnectionString)
            con.Open()
            Dim Sql As String = "SELECT FirstName,LastName,MiddleName FROM contacts WHERE  Gender = @Gender AND Age = @Age"
            Using cmd As New SqlClient.SqlCommand(Sql, con)
                cmd.Parameters.AddWithValue("@Gender", ListView1.FocusedItem.Text)
                cmd.Parameters.AddWithValue("@Age", ListView1.FocusedItem.SubItems(2).Text)
                cmd.CommandTimeout = 3600
                cmd.CommandType = CommandType.Text
                Using DataRead As SqlClient.SqlDataReader = cmd.ExecuteReader
                    If DataRead.HasRows Then
                        lblLoading.Visible = False
                        While DataRead.Read
                            count += 1
                            ListView2.Items.Add(Trim(DataRead(0).ToString))
                            ListView2.Items(ListView2.Items.Count - 1).SubItems.Add(Trim(DataRead(1).ToString))
                            ListView2.Items(ListView2.Items.Count - 1).SubItems.Add(Trim(Format(DataRead(2), "yyyy-MM-dd")))
                        End While
                        lblItem.Visible = True
                        lblItem.Text = countitem & " Item/s"
                    End If
                    DataRead.Close()
                End Using
            End Using
            con.Close()
        End Using
btnPrint.Enabled = ListView2.Items.Count > 1
End Sub


How do I Implement Threading with out using
CheckForIllegalCrossThreadCalls = False
It give exemption of CrossThreading

PS: If you have better way of my coding feel free to post :)
Posted

1 solution

Well, the point is that you try to change your ListView2 from a thread which is not the main (UI) thread - and that's not allowed at all.
A solution could be to load the data from the database into a collection object in your new thread, then signal that the data are available, and then add the data to the ListView in the main thread (use InvokeRequired and Invoke).
Another point for optimization are the BeginUpdate and EndUpdate methods of the listview. And you could eventually use the VirtualMode of the list view. Or limit the number of result items with a LIMIT 100 clause at the end of your sql statement...
 
Share this answer
 
v2
Comments
iMaker.ph 19-Jul-13 8:26am    
how about the buttons ? am i doing it right I get CrossThreading exemptions :(
iMaker.ph 19-Jul-13 23:01pm    
I'd Insert my DataRead with this statement

If InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf LoadToAnotherListView)
Else
Using DataReader

End Using
End If

I don't get the Cross Threading error Am I doing it correct ?
I've observe that my Application still goes to Not Responding :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900