C#的ObjectDisposedException是由用户code未处理 [英] C#: ObjectDisposedException was unhandled by user code

查看:221
本文介绍了C#的ObjectDisposedException是由用户code未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个秘密的项目我的工作,基本上他们做的是使用Web浏览器填写表单,然后点击提交去一个页面。

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.

昨天一切工作正常,但我今天的界面一直落后每次的code到数据填写在网站上的表格开始。

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

在C#IDE这是我得到的错误:

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

C#的ObjectDisposedException是由用户code未处理

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...?

推荐答案

假设

webBrowser1

是类型

System.Windows.Forms.WebBrowser

然后简单的错误意味着你已经丢弃了webBrowser1对象,调用你code的其余部分之前。因此,解决方法是只需确保在适当的时候处置的对象。

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.

您的申报对象webBrowser1 using块内将范围更加明确,例如

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);

}

将有助于确保你俩妥善处置WebBrowser对象的(因为它是资源密集型),而它是积极的,你只使用它(因为它是用块内只访问)。

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).

using块还有助​​于确保即使在发生异常时妥善处置。

这篇关于C#的ObjectDisposedException是由用户code未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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