Question Threading and UI updating help.

Herman

Well-known member
Joined
Oct 18, 2011
Messages
883
Location
Montreal, QC, CA
Programming Experience
10+
Hello fellow codemonkeys... I have a small problem that I am sure is due mostly to my inexperience with threading. I have used simple entirely self-contained threads before, but not to a great extent. I understand the basic laws that surround them, such as how a thread cannot access another thread's member directly, and must use delegates.

Consider the following chunk of code:
VB.NET:
    ' This sub refreshes the server list.
    Private Sub RefreshServerList()
        ' Update the status bar.
        UpdateProgressBar("Refreshing server list...", 50, 100)
        ' Clear the server list.
        lstServers.Items.Clear()
        lstServers.SelectedIndex = -1
        ' Instanciate the SQL data source enumerator.
        Dim SQLInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
        Dim dataInstanceList As System.Data.DataTable = SQLInstance.GetDataSources()
        ' Populate the server list.
        For Each Row As DataRow In dataInstanceList.Rows
            lstServers.Items.Add(Row(0).ToString & "\" & Row(1).ToString)
        Next
        ' Clear the status bar.
        UpdateProgressBar("", 0, 100)
    End Sub

I have been scratching my head as to how I should devise a threading strategy so that these statements are executed in their own thread:
VB.NET:
        Dim SQLInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
        Dim dataInstanceList As System.Data.DataTable = SQLInstance.GetDataSources()

As you see, the problematic is the Datatable return object, which needs to be accessed from the calling thread.

Any help would be greatly appreciated.

A little extra background... This method is part of my form code, and the goal I wish to achieve through threading these statements is greater UI responsiveness. I deliberately took the simplest method I could find. Am I going about this the wrong way?
 
I understand the basic laws that surround them, such as how a thread cannot access another thread's member directly, and must use delegates.
I don't think you understand them as well as you think, because threads don't have members. You can access any variable, property or method that is within scope from any thread at any time. That is a matter of scope, which threads do not affect. The issue is that, if you access any member of a control that makes use of the control's handle and that handle was not created on the current thread, unpredictable behaviour can result.

You say that you know that you need to use a delegate, which you invoke on the thread that owns the control you want to update. That's all you have to do here. You simply invoke a method with a parameter of the appropriate type and a delegate to match. You can then pass that data across the thread boundary. For instructions on how, click here.

Alternatively, you could use a BackgroundWorker. For an example of how, click here.
 
Back
Top