tags:

views:

55

answers:

2

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  A: 

asdf.Close should calls asdf.Dispose.

Jonathan Allen
Even when using Dispose, I'm still not able to close the form the second time...
Soo
A: 

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();
        }
Eric Dahlvang
Sorry for not including enough code, but the list item is removed in a different part of the code, otherwise you are correct
Soo