Is it possible to test if a WebBrowser.Document has been disposed so that I don't keep getting ObjectDisposedException's?
I know the following code will do the job, but I'd prefer to test for the Document being disposed rather than having to catch it. Any thoughts?
private Size GetContentSize()
{
try
{
if (
this.webBrowser.Document != null
&&
this.webBrowser.Document.Body != null)
{
return this.webBrowser.Document.Body.ScrollRectangle.Size;
}
else
{
return Size.Empty;
}
}
catch (ObjectDisposedException)
{
return Size.Empty;
}
}
The type of WebBrowser.Document is HtmlDocument. It doesn't have a Dispose() method.
The more likely source of the exception is the WebBrowser itself. It has an IsDisposed property that you could use. However, I'd strongly recommend you go looking for the bug in the code instead of applying that bandaid. Perhaps a stray using statement.