abhimanyu wrote:
I have a method Marshal.IsComObject(...) that returns TRUE if an
object is a COM object.
I have an object of Excel.Worksheet that I released using
Marshal.ReleaseComObject(...). I want to iterate over a collection of
Excel.Worksheet and find out which is disposed.
There's no way to determine whether an object is disposed other than using
it and getting an ObjectDisposedException. And that's actually the *good*
scenario. If you were working with pure COM, you'd have no way at all of
knowing whether an interface pointer was valid, and you'd likely just get a
crash if you use an invalid one.
You must keep track of when an object was disposed yourself. In general, do
not dispose objects until you're certain (or have made certain) that nobody
will use them anymore, then you can omit keeping track.
Currently the workaround for me (bad one) is
try
{
string name = worksheet.Name;
}
catch(Exception e)
Catch specific exception types, not Exception. The call to worksheet.Name
could fail for multiple reasons, not all of which should simply be silently
discarded.
{
// this one was release as I cannot access it
worksheet = null;
}
Is there any other method through which I can check for a VALID(not
released) COM object?
If you know you released the object, why do you need to check for it here?
Somewhere inbetween you discarded information or failed to propagate it.
Why don't you set the reference to null when you release it, rather than
when you find out it was released? If the worksheet is in a collection, why
don't you remove it from the collection?
--
J.
http://symbolsprose.blogspot.com