Results 1 to 12 of 12

Thread: Help from a VBS to VB (noob)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Help from a VBS to VB (noob)

    First off I apologize for not being the expert in VB. I download VB and created a form with a webbrowser component. So, now my question comes down to the code being used.

    Here is my .vbs, is there an easy way to place this code but in VB. I'm assuming something in the web browser component code field?

    Set objIE = CreateObject("InternetExplorer.Application")
    Dim http: set http= CreateObject("MSXML2.XMLHTTP")
    http.open "GET", "http://www.thisisatestandwillfail2.co", false
    http.send

    If http.status = 200 then
    ' if return code equals something else, launch IE and point to another url
    Wscript.Quit ' I would like it to load a local html file instead of quiting.
    Else

    objIE.Visible = True ' Set browser object visible
    objIE.Navigate "http://www.thisisworking.com"
    Wscript.Sleep(60000) ' Sleep for 60 seconds
    objIE.Quit
    End If


    Any help is appreciated!

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Re: Help from a VBS to VB (noob)

    Instead of doing the above, could I just do a webbrowser.navigate to the URL and track the title bar? If the title bar is not equal to a certain string sleep for a few seconds and quit?

    I'm just confused on how to track the title bar?

  3. #3
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Help from a VBS to VB (noob)

    Assuming you are using VB.NET then here is an example of navigating to a URL and displaying the title:
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'This is how you can navigate to a URL within the WebBrowser control
    5.         WebBrowser1.Navigate("www.your-url.co.uk")
    6.     End Sub
    7.  
    8.     'This event will run when the WebPage has finished navigating.
    9.     Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    10.         'This would display the Title of the loaded document in a Message box.
    11.         MessageBox.Show(WebBrowser1.DocumentTitle)
    12.     End Sub
    13.  
    14. End Class
    Hope this helps

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Re: Help from a VBS to VB (noob)

    Thanks that worked!

    Should I start a new thread or does anyone know the best way for .net repeat command? I'm trying to do this command every five seconds.

    webbrowser3.url = new uri("http://www.goodsite.com")

    if webbrowser3.DocumentTitle = ("good title bar")

    Do something


    Basically, I just need to repeat the command until I receive a good response. Once I receive a good response I will act upon it.

    Any help is appreciated! Thanks again.

  5. #5
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Help from a VBS to VB (noob)

    Perhaps it would be best to use the WebClient class while you look for a Website with a "Good Title Bar". The WebClient runs 'behind the scenes' and can retrieve the WebPage Source Code.

    Because it can take a while to load a webpage you can retrieve the WebSite Source in a BackGroundWorker to prevent the form from locking up. Here is an example that demonstrates how it could work:
    VB.NET Code:
    1. Option Strict On
    2.  
    3. Imports System.ComponentModel
    4. Imports System.Net
    5. Imports System.IO
    6.  
    7. Public Class Form1
    8.  
    9.     'Create a BackGroundWorker
    10.     Private WithEvents BackGroundWorker1 As New BackgroundWorker
    11.  
    12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.  
    14.         'Create a list of links you want to check
    15.         Dim LinksToLoop As New List(Of String)
    16.         'Add links to the list
    17.         LinksToLoop.Add("http://www.goodsite1.com")
    18.         LinksToLoop.Add("http://www.goodsite2.com")
    19.         LinksToLoop.Add("http://www.goodsite3.com")
    20.  
    21.         'Create a WebClient
    22.         Dim wwwClient As New WebClient
    23.  
    24.         'Loop through your links
    25.         For Each Link As String In LinksToLoop
    26.             'Pass the link and the webclient into the BackGroundWorker as object arguments
    27.             BackGroundWorker1.RunWorkerAsync(New Object() {wwwClient, Link})
    28.  
    29.             'This will stop the form from locking up while the BackGroundWorker is running
    30.             Do While BackGroundWorker1.IsBusy
    31.                 Application.DoEvents()
    32.             Loop
    33.         Next Link
    34.  
    35.     End Sub
    36.  
    37.     Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackGroundWorker1.DoWork
    38.  
    39.         Try
    40.             'Get the arguments (the URL and WebClient control)
    41.             Dim Arguments() As Object = CType(e.Argument, Object())
    42.             'Cast the URL back to a string from an object
    43.             Dim URL As String = DirectCast(Arguments(1), String)
    44.  
    45.             'Check that a URL was provided
    46.             If Not String.IsNullOrEmpty(URL) Then
    47.  
    48.                 'Cast the WebClient back from an Object
    49.                 Dim wwwClient As WebClient = DirectCast(Arguments(0), WebClient)
    50.  
    51.                 'Navigate the WebClient to the URL and Read the HTML Source from the WebClient
    52.                 Dim HTMLStream As Stream = wwwClient.OpenRead(URL)
    53.                 Dim SR As New StreamReader(HTMLStream)
    54.                 Dim HTML As String = SR.ReadToEnd
    55.  
    56.                 'Retrieve the text within the <title></title> tags
    57.                 Dim TitleStartIndex As Integer = HTML.IndexOf("<title")
    58.                 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)
    59.                 Dim TitleEndIndex As Integer = HTML.IndexOf("</title>")
    60.                 Dim PageTitle As String = HTML.Substring(TitleStartIndex, TitleEndIndex - TitleStartIndex)
    61.  
    62.                 'Pass the Page Title to the Result property
    63.                 e.Result = PageTitle
    64.             Else
    65.                 'If an empty URL was entered then set the result to nothing
    66.                 e.Result = Nothing
    67.             End If
    68.         Catch ex As Exception
    69.             'If there were any errors then set the result to nothing
    70.             e.Result = Nothing
    71.         End Try
    72.  
    73.     End Sub
    74.  
    75.     'This event will occur once the code in a BackGroundWorker has completed
    76.     Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackGroundWorker1.RunWorkerCompleted
    77.         'If the result is nothing then there was an error
    78.         If Not (e.Result) Is Nothing Then
    79.  
    80.             'The Page title can be found in e.Result e.g.
    81.             MessageBox.Show(e.Result.ToString)
    82.  
    83.             'You can perform your extra logic here e.g.
    84.             If e.Result.ToString = "Good Title Bar" Then
    85.                 'Do something
    86.             End If
    87.  
    88.         End If
    89.     End Sub
    90. End Class
    Hope this helps

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Re: Help from a VBS to VB (noob)

    Thanks again, I have it working. I was unaware of the background worker.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Re: Help from a VBS to VB (noob)

    I decided to loop the process so it will continue to look for the site until it comes back ok. It's using quite a bit of CPU. Is that normal? Any possible way to force it to only use a certain &#37; of CPU?

    Do Until BackGroundWorker1.CancellationPending
    'Making the links into string.
    Dim LinksToLoop As New List(Of String)
    'Add links to be checked.
    LinksToLoop.Add("http://www.goodsite.com")
    'Create a WebClient. This is opening up goodsite website.
    Dim wwwClient As New WebClient
    'Loop through your links. You can add more than one link if we so desire.
    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
    'Will loop into the next link if we add one.
    Next Link

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Help from a VBS to VB (noob)

    What you have is a busy wait, which is horribly inefficient, as you have found.

    The thing to do is to add a timer component onto the form. Set the timer interval to whatever you feel is a reasonable waiting period (you suggested 5s, which means you would set the interval to 5000), and in the timer Tick event, that is where you perform your actions. The timer Tick event will be raised every time the interval elapses. Therefore, if the interval is 5000 (which is in milliseconds, so that would be 5 seconds), the Tick event will be raised every 5 seconds until you disable the timer. The only concern would be that you want the interval to be longer than the work you are doing. Therefore, if you think that the operation might take longer than 5s, use a longer interval.
    My usual boring signature: Nothing

  9. #9
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Help from a VBS to VB (noob)

    I just tested the code in a loop and get the same effect. If you also make use of the System.Threading.Thread.Sleep method where I originally used Application.DoEvents to prevent the UI locking up, then the CPU usage will improve.

    Here is how the updated code would look:
    VB.NET Code:
    1. 'This will stop the form from locking up while the BackGroundWorker is running
    2. Do While BackGroundWorker1.IsBusy
    3.     Application.DoEvents()
    4.     System.Threading.Thread.Sleep(250)
    5. Loop
    I will let you know if I come accross a better way to wait for a thread to finish. Or as Shaggy Hiker pointed out you can use a Timer function.

  10. #10
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Help from a VBS to VB (noob)

    Here is a better approach than my first example. It uses a System.Threading.Thread instead of the BackGroundWorker. You can use the AutoResetEvent class to wait for threads to finish before continuing with code execution.
    VB.NET Code:
    1. Option Strict On
    2.  
    3. Imports System.Net
    4. Imports System.IO
    5. Imports System.Threading
    6.  
    7. Public Class Form1
    8.  
    9.     'Create a Shared string that can be accessed from another thread
    10.     Private Shared WebsiteTitle As String = Nothing
    11.     'The AutoResetEvent Class contains methods to wait for an event to finish
    12.     Dim WaitEvent As AutoResetEvent = New AutoResetEvent(False)
    13.  
    14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.  
    16.         'Create a list of links you want to check
    17.         Dim LinksToLoop As New List(Of String)
    18.         'Add links to the list
    19.         LinksToLoop.Add("http://www.Link1.co.uk")
    20.         LinksToLoop.Add("http://www.Link2.com")
    21.         LinksToLoop.Add("http://www.Link3.com")
    22.         LinksToLoop.Add("http://www.Link4.co.uk")
    23.  
    24.         'Create a WebClient
    25.         Dim wwwClient As New WebClient
    26.  
    27.         'Loop through your links
    28.         For Each Link As String In LinksToLoop
    29.             'Create a new thread to run the Retrieve_WebSource sub in the background
    30.             Dim WebBrowserThread As New Thread(AddressOf Retrieve_WebTitle)
    31.             'Start the thread and pass in the WebClient and Link as object arguments
    32.             WebBrowserThread.Start(New Object() {wwwClient, Link})
    33.  
    34.             'Wait for the Thread to finish
    35.             WaitEvent.WaitOne()
    36.  
    37.             'Check if a WebSite Title was found
    38.             If Not (WebsiteTitle) = Nothing Then
    39.  
    40.                 'You can perform your extra logic here e.g.
    41.                 If WebsiteTitle = "Good Title Bar" Then
    42.                     MessageBox.Show(WebsiteTitle)
    43.                 End If
    44.  
    45.             End If
    46.         Next Link
    47.  
    48.     End Sub
    49.  
    50.  
    51.     Private Sub Retrieve_WebTitle(ByVal Arguments As Object)
    52.  
    53.         Try
    54.             'Get the arguments (the URL and WebClient control)
    55.             Dim Args() As Object = CType(Arguments, Object())
    56.             'Cast the URL back to a string from an object
    57.             Dim URL As String = DirectCast(Args(1), String)
    58.  
    59.             'Check that a URL was provided
    60.             If Not String.IsNullOrEmpty(URL) Then
    61.  
    62.                 'Cast the WebClient back from an Object
    63.                 Dim wwwClient As WebClient = DirectCast(Args(0), WebClient)
    64.  
    65.                 'Navigate the WebClient to the URL and Read the HTML Source from the WebClient
    66.                 Dim HTMLStream As Stream = wwwClient.OpenRead(URL)
    67.                 Dim SR As New StreamReader(HTMLStream)
    68.                 Dim HTML As String = SR.ReadToEnd
    69.  
    70.                 'Retrieve the text within the <title></title> tags
    71.                 Dim TitleStartIndex As Integer = HTML.IndexOf("<title")
    72.                 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)
    73.                 Dim TitleEndIndex As Integer = HTML.IndexOf("</title>")
    74.                 Dim PageTitle As String = HTML.Substring(TitleStartIndex, TitleEndIndex - TitleStartIndex)
    75.  
    76.                 'Update the Shared WebSiteTitle variable with the result
    77.                 WebsiteTitle = PageTitle
    78.             Else
    79.                 'If an empty URL was entered then the WebSiteTitle variable should be set to nothing
    80.                 WebsiteTitle = Nothing
    81.             End If
    82.  
    83.             'Changes the state of the event to signaled (meaning waiting threads can proceed)
    84.             WaitEvent.Set()
    85.         Catch ex As Exception
    86.             'If there were any errors then then the WebSiteTitle variable should be set to nothing
    87.             WebsiteTitle = Nothing
    88.             WaitEvent.Set()
    89.         End Try
    90.  
    91.     End Sub
    92.  
    93. End Class
    Last edited by JayJayson; Sep 13th, 2011 at 05:52 AM.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    16

    Re: Help from a VBS to VB (noob)

    Yep, I went ahead with the timer.

    I apologize for this stupid question but a little confused. Why would the case of the <title> matter when scrubbing the data? If the html document is labeled TITLE or Title it fails. I thought converting it to string like

    Dim PageTitle As String = HTML.Substring(TitleStartIndex, TitleEndIndex - TitleStartIndex)

    would help but I guess not.
    Last edited by titanmifi; Sep 13th, 2011 at 01:52 PM.

  12. #12
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Help from a VBS to VB (noob)

    Try this instead:

    Add the following statement to the top of your form (outside the Class tags):
    VB.NET Code:
    1. Imports System.Globalization
    And replace the previous code with this:
    VB.NET Code:
    1. 'Retrieve the text within the <title></title> tags
    2. Dim GoCompare As CompareInfo = CultureInfo.InvariantCulture.CompareInfo
    3. Dim TitleStartIndex As Integer = GoCompare.IndexOf(HTML, "<title", CompareOptions.IgnoreCase)
    4. 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)
    5. Dim TitleEndIndex As Integer = GoCompare.IndexOf(HTML, "</title>", CompareOptions.IgnoreCase)
    6. Dim PageTitle As String = HTML.Substring(TitleStartIndex, TitleEndIndex - TitleStartIndex)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width