0

I am using the mshtml library inside a VB.NET console application to extract some data from an http request.

The code is as follows:

Dim htmlDocument As IHTMLDocument2
For i As Integer = 0 To 10
    searchHtml = getHtml(url)
    htmlDocument = New HTMLDocumentClass()
    htmlDocument.write(searchHtml)
    htmlDocument.close()
    Dim results As IHTMLElement = htmlDocument.body.all.item("ires")
    For Each li As IHTMLElement In results.all.tags("li")
        Dim element As IHTMLElement = li.all.tags("cite")(0)
        If element.innerText.ToLower().Contains(text) Then
            ' Do Something here
            Exit For
        End If
    Next
Next

The code above is called recursively from another method.

I can run my code in debug and it works time after time with no problem.

If I compile the exe and run it then I receive one of two errors. Initially I got an "Object reference not set to an instance of an object" after 11 calls to the above. This is repeatable and always occurs after 11 iterations.

If I add a Threading.Thread.Sleep(1000) before the above and re-compile I get the "Access is denied" error, again after a certain number of iterations.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
AGB
  • 2,378
  • 21
  • 37
  • 2
    Use the HTML Agility Pack instead. – SLaks Dec 04 '12 at 17:11
  • Which line throws the exception? – Steven Doggart Dec 04 '12 at 17:15
  • Pretty undiagnosable behavior. Start by putting [STAThread] on your main method. Consider an occasional call to GC.Collect() next. – Hans Passant Dec 04 '12 at 17:38
  • It is pretty un-diagnosable behaviour....quite annoying really! Good shout on the garbage collector but I think I'm going to take a look at the HTML Agility Pack first. It sounds like it is a lot nicer to use than the mshtml library. – AGB Dec 05 '12 at 09:21
  • HTML Agility Pack is excellent; much easier to work with (love the XPath support) and I'm no longer getting the error. Thanks for the heads up SLaks. If you answer as such I'll mark it. – AGB Dec 05 '12 at 10:32

0 Answers0