Confused with Threads

dagan

New member
Joined
Oct 29, 2009
Messages
3
Programming Experience
3-5
Hi All,
I have this little code snippet - that trawls through some site and just like a spider gets the HTTP requests...

This should do for about 100 or so sites - so I have created a progress bar and tied it with a thread so the process can run in the background - and not lag the whole programme (and also I could show progress neatly with the bar) - however when executed (although I get the progress bar coming up nicely) - the program still seems to lag...

What am I doing wrong???

here is the code:
VB.NET:
Private Sub AllSitesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllSitesToolStripMenuItem.Click
        Dim tbladpt As New SiteCheckDataSetTableAdapters.SitesTableAdapter()
        Dim tbl As New SiteCheckDataSet.SitesDataTable()
        tbl = tbladpt.GetData()
        Progress.progressBar.Value = 0
        Progress.progressBar.Maximum = tbl.Rows.Count
        Progress.Show()

        Progress.t = New Thread(AddressOf ScanSites)
        Progress.t.Start()
    End Sub

    Public Sub ScanSites()
        Dim tbladpt As New SiteCheckDataSetTableAdapters.SitesTableAdapter()
        Dim tbl As New SiteCheckDataSet.SitesDataTable()
        Dim d As New SiteScanDelegate(AddressOf SiteScan)
        tbl = tbladpt.GetData()
        For Each rs As DataRow In tbl
            Invoke(d, New String() {rs("domain")})
        Next
        Invoke(d, New String() {"FinishedJob"})
    End Sub

    Delegate Sub SiteScanDelegate(ByVal str As String)

    Public Sub SiteScan(ByVal url As String)
        If url = "FinishedJob" Then
            Progress.Hide()
        Else
            Dim str As String = ""
            If InStr(url, "http://") = 0 Then url = "http://" & url
            Dim request As HttpWebRequest = WebRequest.Create(url)
            Try
                Dim response As HttpWebResponse = request.GetResponse()
                Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
                str = reader.ReadToEnd()
                reader.Close()
            Catch ex As System.Exception
                str = ""
            Finally
                Progress.progressBar.Value += 1
            End Try
        End If
    End Sub
 
Control.Invoke means you're calling the method on UI thread, so ScanSites() is done in worker thread and all SiteScan(String) calls is done in UI thread.
 
I would suggest that you use a BackgroundWorker for this. It's designed specifically for situations like this. You call RunWorkerAsync and then handle the DoWork event. You do your background work in the DoWork event handler and when you want to update the UI you call ReportProgress and handle the ProgressChanged event. In the ProgressChanged event handler you update the UI. When the work is done you can handle the RunWorkerCompleted event and update the UI one last time in the RunWorkerCompleted event handler.
 
Back
Top