Option Strict On
Imports System.ComponentModel
Imports System.Net
Imports System.IO
Public Class Form1
'Create a BackGroundWorker
Private WithEvents BackGroundWorker1 As New BackgroundWorker
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create a list of links you want to check
Dim LinksToLoop As New List(Of String)
'Add links to the list
LinksToLoop.Add("http://www.goodsite1.com")
LinksToLoop.Add("http://www.goodsite2.com")
LinksToLoop.Add("http://www.goodsite3.com")
'Create a WebClient
Dim wwwClient As New WebClient
'Loop through your links
For Each Link As String In LinksToLoop
'Pass the link and the webclient into the BackGroundWorker as object arguments
BackGroundWorker1.RunWorkerAsync(New Object() {wwwClient, Link})
'This will stop the form from locking up while the BackGroundWorker is running
Do While BackGroundWorker1.IsBusy
Application.DoEvents()
Loop
Next Link
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackGroundWorker1.DoWork
Try
'Get the arguments (the URL and WebClient control)
Dim Arguments() As Object = CType(e.Argument, Object())
'Cast the URL back to a string from an object
Dim URL As String = DirectCast(Arguments(1), String)
'Check that a URL was provided
If Not String.IsNullOrEmpty(URL) Then
'Cast the WebClient back from an Object
Dim wwwClient As WebClient = DirectCast(Arguments(0), WebClient)
'Navigate the WebClient to the URL and Read the HTML Source from the WebClient
Dim HTMLStream As Stream = wwwClient.OpenRead(URL)
Dim SR As New StreamReader(HTMLStream)
Dim HTML As String = SR.ReadToEnd
'Retrieve the text within the <title></title> tags
Dim TitleStartIndex As Integer = HTML.IndexOf("<title")
TitleStartIndex = HTML.IndexOf(">"c, TitleStartIndex) + ">".Length 'This line will help in cases where an element is found in the <title> tag e.g. <title id="someID"> (It happens)
Dim TitleEndIndex As Integer = HTML.IndexOf("</title>")
Dim PageTitle As String = HTML.Substring(TitleStartIndex, TitleEndIndex - TitleStartIndex)
'Pass the Page Title to the Result property
e.Result = PageTitle
Else
'If an empty URL was entered then set the result to nothing
e.Result = Nothing
End If
Catch ex As Exception
'If there were any errors then set the result to nothing
e.Result = Nothing
End Try
End Sub
'This event will occur once the code in a BackGroundWorker has completed
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackGroundWorker1.RunWorkerCompleted
'If the result is nothing then there was an error
If Not (e.Result) Is Nothing Then
'The Page title can be found in e.Result e.g.
MessageBox.Show(e.Result.ToString)
'You can perform your extra logic here e.g.
If e.Result.ToString = "Good Title Bar" Then
'Do something
End If
End If
End Sub
End Class