Background Functions

PutterPlace

Active member
Joined
Feb 18, 2008
Messages
37
Programming Experience
1-3
I am in the process of working on a project. It's an ID collector for a website that belongs to a friend of mine. My main problem right now is when the "Start" button is pressed. Here's what I have right now:

VB.NET:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
   btnStart.Enabled = False
   btnStop.Enabled = True
   btnBrowse.Enabled = False

   Dim sURL As String = "http://www.someURL.com/dir/"
   Dim oRequest As HttpWebRequest
   Dim oStream As Stream
   Dim sTemp As String
   oRequest = (HttpWebRequest.Create(sURL))
   Dim oResponse As WebResponse = oRequest.GetResponse()
   oStream = oResponse.GetResponseStream
   sTemp = New StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oResponse.Close()
   Dim OriginalString As String = sTemp
   Dim StartMatch As String = "<br><a href='/directory/?act="
   Dim EndMatch As String = "'>"
   Dim TheMatches As New List(Of String)
   
   While OriginalString.IndexOf(StartMatch) >= 0
      OriginalString = OriginalString.Substring(OriginalString.IndexOf(StartMatch) + StartMatch.Length)
      TheMatches.Add(OriginalString.Substring(0, OriginalString.IndexOf(EndMatch)))
   End While
   
   For Each _Match As String In TheMatches
      ListBox1.Items.Add(_Match)
      Label1.Text = "Collected IDs: " & ListBox1.Items.Count
   Next _Match
   
   btnStart.Enabled = True
   btnStop.Enabled = False
   btnBrowse.Enabled = True
End Sub

My main problem is that the UI seems to freeze after the start button is clicked. Then it "unfreezes" when this process has finished. Is there anything that I can do to fix this problem? I added the "Stop" button so that I can stop everything in the middle of the process if needed. But if the UI is frozen until the process is complete, it doesn't really help much.

I tried putting the following into a BackgroundWorker:

VB.NET:
   Dim sURL As String = "http://www.someURL.com/dir/"
   Dim oRequest As HttpWebRequest
   Dim oStream As Stream
   Dim sTemp As String
   oRequest = (HttpWebRequest.Create(sURL))
   Dim oResponse As WebResponse = oRequest.GetResponse()
   oStream = oResponse.GetResponseStream
   sTemp = New StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oResponse.Close()
   Dim OriginalString As String = sTemp
   Dim StartMatch As String = "<br><a href='/directory/?act="
   Dim EndMatch As String = "'>"
   Dim TheMatches As New List(Of String)
   
   While OriginalString.IndexOf(StartMatch) >= 0
      OriginalString = OriginalString.Substring(OriginalString.IndexOf(StartMatch) + StartMatch.Length)
      TheMatches.Add(OriginalString.Substring(0, OriginalString.IndexOf(EndMatch)))
   End While
   
   For Each _Match As String In TheMatches
      ListBox1.Items.Add(_Match)
      Label1.Text = "Collected IDs: " & ListBox1.Items.Count
   Next _Match

However, I wasn't able to update the UI because the objects were created in a different thread:

VB.NET:
      ListBox1.Items.Add(_Match)
      Label1.Text = "Collected IDs: " & ListBox1.Items.Count

Any help would be greatly appreciated. :)
 
Use a BackGroundWorker or any other of the vast multithreading functionalities that exist in the .Net library to do the work. Never do anything in UI thread that takes time.
 
I tried putting the following into a BackgroundWorker:

VB.NET:
   Dim sURL As String = "http://www.someURL.com/dir/"
   Dim oRequest As HttpWebRequest
   Dim oStream As Stream
   Dim sTemp As String
   oRequest = (HttpWebRequest.Create(sURL))
   Dim oResponse As WebResponse = oRequest.GetResponse()
   oStream = oResponse.GetResponseStream
   sTemp = New StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oResponse.Close()
   Dim OriginalString As String = sTemp
   Dim StartMatch As String = "<br><a href='/directory/?act="
   Dim EndMatch As String = "'>"
   Dim TheMatches As New List(Of String)
   
   While OriginalString.IndexOf(StartMatch) >= 0
      OriginalString = OriginalString.Substring(OriginalString.IndexOf(StartMatch) + StartMatch.Length)
      TheMatches.Add(OriginalString.Substring(0, OriginalString.IndexOf(EndMatch)))
   End While
   
   For Each _Match As String In TheMatches
      ListBox1.Items.Add(_Match)
      Label1.Text = "Collected IDs: " & ListBox1.Items.Count
   Next _Match

However, I wasn't able to update the UI since it was created in a different thread. Updating the UI is an important task because I need to add items to a blank list box. Any suggestions?
 
Don't give up just yet :D You can send your matches as Result to RunWorkerCompleted event or as progress values to the ProgressChanged event, both which is safe to access UI. To read/write Result use e.Result.
 
Back
Top