问题
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...?
回答1:
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.
来源:https://stackoverflow.com/questions/7923893/c-objectdisposedexception-was-unhandled-by-user-code