C#:ObjectDisposeException 未被用户代码处理

发布于 2024-12-12 07:53:35 字数 328 浏览 4 评论 0原文

我正在开发一个秘密项目,基本上它的作用是使用网络浏览器进入页面,填写表格,然后单击提交。

昨天一切正常,但今天每次启动向网站上的表单填写数据的代码时,我的界面都会滞后。

在 C# IDE 中,这是我收到的错误:

C#:ObjectDisposeException 未由用户代码处理

......当我查看它的详细信息时,我得到:

名称“$exception”在当前上下文中不存在

有人知道我必须做什么吗?我必须扔掉一些东西还是……?

I have a secret project I am working on and basically what it does is go to a page using the web browser fills out the form then clicks submit.

Yesterday everything was working OK but today my interface keeps lagging everytime the code to fill out data to forms on the site starts.

In the C# IDE this is the error I am getting:

C#: ObjectDisposedException was unhandled by user code

...and when I view the details of it I get:

The name '$exception' does not exist in the current context

Anybody have any idea of what I have to do? Do I have to dispose something or...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-12-19 07:53:35

假设它

webBrowser1

是类型

System.Windows.Forms.WebBrowser

,那么该错误仅仅意味着您在调用其余代码之前已经释放了 webBrowser1 对象。因此,解决方法就是确保在适当的时间处理该对象。

在 using 块内声明您的 webBrowser1 对象将使范围更加明确,例如

using(System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser())
{

    //put calls to your functionality here e.g.
    webBrowser1.Document.GetElementById("oauth_signup_client_fullname")
       .SetAttribute("‌​value", txtBoxImportNames1.Text + txtBoxImportNames2.Text);

    //or pass it to another function, and it will still get disposed correctly, e.g.
    myOtherFunctionality(webBrowser1);

}

将有助于确保您适当地处理 WebBrowser 对象(因为它是资源密集型的)并且您仅在它处于活动状态时才使用它(因为它是只能在 using 块内访问)。

using 块还有助于确保即使在发生异常时也能正确处理。

Assuming that

webBrowser1

is of type

System.Windows.Forms.WebBrowser

then the error simply means that you already disposed the webBrowser1 object, prior to calling the rest of your code. So the fix is to simply make sure you dispose of the object at the appropriate time.

Declaring your webBrowser1 object inside of a using block will make the scope more explicit, e.g.

using(System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser())
{

    //put calls to your functionality here e.g.
    webBrowser1.Document.GetElementById("oauth_signup_client_fullname")
       .SetAttribute("‌​value", txtBoxImportNames1.Text + txtBoxImportNames2.Text);

    //or pass it to another function, and it will still get disposed correctly, e.g.
    myOtherFunctionality(webBrowser1);

}

will help ensure that you both dispose of the WebBrowser object appropriately (since it is resource intensive) and that you only use it while it is active (since it is only accessible within the using block).

The using block also helps ensure proper disposal even when an exception occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文