locked
Cannot Access a Disposed Object Error RRS feed

  • Question

  • I have a WinForms app which has been running without any issues till date. All of a sudden, when I ran the app today, I am getting this error:

    Cannot Access a Disposed Object  "\Object Name"\

     

    This is occuring specifically at this line of code:

    System.Windows.Forms.Form f = new System.Windows.Forms.Form();

    return System.Windows.Forms.MessageBox.Show(f, text, caption, buttons, icon);

     

    I am not sure why this should occur now, given the fact that it was working all these days.

     

    Wednesday, July 28, 2010 9:02 PM

Answers

  • You cannot call Show on a  previously shown and closed form. If you need that, handle the Closing event on the child form, and hide it instead of actually closing it.
    http://blog.voidnish.com
    • Proposed as answer by Paras Sanghani Thursday, July 29, 2010 9:36 AM
    • Marked as answer by h_abhijith Tuesday, August 3, 2010 2:22 PM
    Wednesday, July 28, 2010 9:13 PM

All replies

  • You cannot call Show on a  previously shown and closed form. If you need that, handle the Closing event on the child form, and hide it instead of actually closing it.
    http://blog.voidnish.com
    • Proposed as answer by Paras Sanghani Thursday, July 29, 2010 9:36 AM
    • Marked as answer by h_abhijith Tuesday, August 3, 2010 2:22 PM
    Wednesday, July 28, 2010 9:13 PM
  • Hi iconabhi,

     

    I agree with Nishant. That error only occurs when the Form is closed(disposed). You can add following check to avoid this kind of error.

    if (f.IsDisposed)

    {

         f = new System.Windows.Forms.Form();

    }

    MessageBox.Show(f, "abc", "bcd", MessageBoxButtons.OK);

     

    Does it work? Please feel free to tell me if you meet any difficulties.

     

    Sincerely,

    Kira Qian

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Proposed as answer by Paras Sanghani Thursday, July 29, 2010 9:36 AM
    Thursday, July 29, 2010 9:35 AM
  • I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, August 2, 2010 9:47 AM
  • Sorry..I was away for a little while now. Yes, I followed Nishanth's solution and looks like it's working now. Thanks for the followup !
    Tuesday, August 3, 2010 2:21 PM