Invoking Something Twice Leads To: “protected override void Dispose”

久未见 提交于 2019-12-12 04:22:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!