WebBrowser throws TargetInvocationException

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I just got a TargetInvocationException from a WebBrowser control,
after calling
WebBrowser.Document.Write(text), which (internally) calls
System.Windows.Forms.HtmlDocument.Write(String text), which calls
System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.Write(Object[]
psarray), which threw ArgumentException, because an index was out of
range in a dictionary object. This was stuffed into a
TargetInvocationException as its InnerException.

So what can we do? WebBrowser seems completely unreliable.

Guess we have to catch all exceptions from it.

Zytan
 
Why does it seem unreliable? By your own admission, an index was out of
range, and an exception was thrown. Also, without seeing any of the code,
it's impossible to say if you were making the calls correctly or not.

You mention an index (of what I have no clue) being out of range. You
shouldn't be relying on an exception being thrown to tell you this. You
should be performing bounds checking yourself.
 
Why does it seem unreliable?

Because I've found about 5 major problems with it, so far, and it is
unreliable. I can't rely on it working properly. :)
By your own admission, an index was out of
range, and an exception was thrown. Also, without seeing any of the code,
it's impossible to say if you were making the calls correctly or not.

Yes, usually it's my fault. But, this time I'm just calling
WebBrowser.Document.Write() to append text to it, over and over, as
I've been doing the past month. This was the first time this
exception was thrown. Oh, and I am asking it to scroll to the bottom
and refresh after each write.
You mention an index (of what I have no clue) being out of range. You
shouldn't be relying on an exception being thrown to tell you this. You
should be performing bounds checking yourself.

The exception and innerexception was all inside of the Write() call,
all within the WebBrowser control. This is the culprit:

System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.Write()

Whoever wrote that code didn't check the bounds.

Regardless, I now catch this exception and do nothing with it, since
what can I do? I'm phasing out the usage of WebBrowser. It just
amazes me how much is wrong with it. But, it is a web browser, after
all, so it is quite complex... I wouldn't want to write it.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top