none
ObjectDisposedException at base.Dispose(disposing); RRS feed

  • Question

  • Hi,

    I have a simple project with a single form. After the user clicks a button, a System.Diagnostics.Process is started. It gives feedback to the form while it's working, and the program wraps up nicely once the process is done.

    If the user closes the program before the process is started, there's no problem. But if the user closes the program while the process is running, the program returns an ObjectDisposedException in the generated Form1.Designer.cs file at following line:

    base.Dispose(disposing);

    I try to kill and dispose the process before the form is being closed:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    Process hbProcess = Process.GetProcessById(ProcessID);
    hbProcess.Kill();
    hbProcess.Dispose();
    }

     

    Details from the exception:

    System.ObjectDisposedException was unhandled
     Message="Geen toegang tot een verwijderd object.\r\nObjectnaam: Form1."
     Source="System.Windows.Forms"
     ObjectName="Form1"
     StackTrace:
        bij System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
        bij System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
        bij System.Diagnostics.Process.ErrorReadNotifyUser(String data)
        bij System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
        bij System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
        bij System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(IMessage msg)
        bij System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        bij System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
        bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        bij System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
        bij System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
        bij System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
        bij System.Diagnostics.Process.OutputReadNotifyUser(String data)
        bij System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
        bij System.Diagnostics.AsyncStreamReader.GetLinesFromStringBuilder()
        bij System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
        bij System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(IMessage msg)
        bij System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        bij System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
        bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        bij System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
        bij System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
     InnerException: 

    Can anyone explain to me why this is happening and how I can solve the issue ?

     

    Thanks in advance!
    Mathew

    Sunday, May 2, 2010 11:08 AM

Answers

  • That is contrary to Microsoft's specifications that Dispose() can be safely called multiple times without generating exceptions.  If the base form is Microsoft's form, then it is probably a bug in the .Net framework.  If the base form is a third party form, then it is most likely a bug in this third party form.
    MCP
    • Marked as answer by SamAgain Tuesday, May 11, 2010 9:33 AM
    Monday, May 3, 2010 1:27 AM

All replies

  • Are you sure ProcessID is the other process? It seems that when your form is trying to clean up behind itself it´s alrady gone...
    Sunday, May 2, 2010 4:25 PM
  • That is contrary to Microsoft's specifications that Dispose() can be safely called multiple times without generating exceptions.  If the base form is Microsoft's form, then it is probably a bug in the .Net framework.  If the base form is a third party form, then it is most likely a bug in this third party form.
    MCP
    • Marked as answer by SamAgain Tuesday, May 11, 2010 9:33 AM
    Monday, May 3, 2010 1:27 AM
  • Hi Mathieu Maes,

        We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, May 11, 2010 9:34 AM
  • To solve this ObjectDisposedException just construct like this:

     

    try

    {

          performAction

    }

    catch (ObjectDisposedException)

    {

          some other action

    }

    Friday, June 11, 2010 7:01 AM