是否有可能测试一个WebBrowser.Document已被释放,这样我就跟不上得到的ObjectDisposedException的? [英] Is it possible to test if a WebBrowser.Document has been disposed so that I don't keep getting ObjectDisposedException's?

查看:106
本文介绍了是否有可能测试一个WebBrowser.Document已被释放,这样我就跟不上得到的ObjectDisposedException的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能测试一个WebBrowser.Document已被释放,这样我就跟不上得到的ObjectDisposedException的?

我知道下面code将做的工作,但我preFER来测试文档被布置,而不是抓住它。有什么想法?

 私人尺寸GetContentSize()
    {
        尝试
        {
            如果(
                this.webBrowser.Document!= NULL
                &功放;&安培;
                this.webBrowser.Document.Body!= NULL)
            {
                返回this.webBrowser.Document.Body.ScrollRectangle.Size;
            }
            其他
            {
                返回Size.Empty;
            }
        }
        赶上(的ObjectDisposedException)
        {
            返回Size.Empty;
        }
    }


解决方案

该型WebBrowser.Document是的HTMLDocument。它不具有一个Dispose()方法

该异常的更可能的来源是web浏览器本身。它有一个IsDisposed属性,你可以使用。然而,我强烈建议你去寻找在code中的漏洞,而不是应用了绷带。也许一个流浪的使用的声明。

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.

这篇关于是否有可能测试一个WebBrowser.Document已被释放,这样我就跟不上得到的ObjectDisposedException的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆