Thread: ObjectDisposedException

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    ObjectDisposedException

    Hello everyone,


    Any general rules/practices to avoid "ObjectDisposedException"-- System.ObjectDisposedException: Cannot access a disposed object?


    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Simple. Never ever try to call a method of a disposed object
    (or, if it's a control, check with the IsDisposed property)
    Last edited by Magos; 05-12-2008 at 06:11 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos,


    Your reply is great! Let me descirbe more details here. I am writing a simple Http Server using HttpListener class in C#.

    1. The exception from server side, when I call EndGetContext, not from client side;
    2. On the server side, I create an HttpListener object, and waiting for incoming client request;
    3. When I received a request, I will process the request asynchronously by using BeginGetContext, and in this way HttpListener object instance can still listen for incoming calls, while processing backend at the same time;
    4. When sending the request to server from client, the client will close the connection immediately -- during the asynchronous processing period;
    5. The server will throw exception when I call EndGetContext. At the same time, ObjectDisposedException will happen.

    Any ideas why the exception will happen? I am also interested in whose Disposed method is called, any ways to detect?

    Quote Originally Posted by Magos View Post
    Simple. Never ever try to call a method of a disposed object
    (or, if it's a control, check with the IsDisposed property)

    regards,
    George

Popular pages Recent additions subscribe to a feed