问题
I have a function that helps me close forms without getting crossthread errors:
public void OutsideClose(long Id)
{
MessageBox.Show("");
if (InvokeRequired)
{
Invoke(new Action<long>(OutsideClose), Id);
}
else
{
var asdf = ListForm.Find(a => a.Id == Id);
if (asdf != null)
{
asdf.Close();
}
}
}
For some reason, if I call this invoke twice, instead of closing the form the second time, it goes to this dispose method:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
I want the form to close, and have no idea what is going on...
回答1:
asdf.Close should calls asdf.Dispose.
回答2:
The form with the id should not be found the second time you call the method.
var asdf = ListForm.Find(a => a.Id == Id);
if (asdf != null)
{
ListForm.Remove(asdf); //or whatever you need to do to remove it...
asdf.Close();
}
来源:https://stackoverflow.com/questions/3233008/invoking-something-twice-leads-to-protected-override-void-dispose