Z
Zytan
There is no IsDisposed() method. I could just access it, and catch
ObjectDisposedException, but that seems ugly.
Zytan
ObjectDisposedException, but that seems ugly.
Zytan
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Zytan said:There is no IsDisposed() method. I could just access it, and catch
ObjectDisposedException, but that seems ugly.
Peter Bromberg said:If it has a Dispose method and it is implemented according to the recommended
pattern, you can call the Dispose method without concern whether it is
Disposed or not, catching the ObjectDisposed exception.
How could we expect to call an "IsDisposed" method on an object that is no
longer there? I'm not sure that makes any sense.
Zytan said:There is no IsDisposed() method. I could just access it, and catch
ObjectDisposedException, but that seems ugly.
Arne Vajhøj said:I do not know if it is "supported" but the following both
seems to work and do make some sense:
sw.BaseStream != null && sw.BaseStream.CanWrite
(actually that is IsNotDisposed() but who cares)
Arne
What are u gonna reproduce? Maybe usin' WeakReference helps u?
keeps application running, but I am uneasy about this.
In my logger, sometimes Finalizers want to call MyLogger.WriteLine()

while there are still references to it, and you could conceivably want
to make sure you don't call other methods (eg Write) on something which
is disposed.
Yes.
I'd argue that it's best not to get into such a situation
in the first place though.
if(sw != null && sw.BaseStream != null)
// go ahead, the underlying resource is still accessible.
else
...
Anyway, I do prefer to handle the ObjectDisposedException.
That's where the problem is. Finalizers are designed to clean up
unmanaged state they know about - and nothing else. As soon as they
start relying on other things being present, you *will* run into
issues.
Personally, I can't remember the last time I wrote a finalizer. Cast
off your C++ idioms![]()
will be called at all.
Read this http://groups.google.com/group/microsoft.public.dotnet.languages.csha...
I suppose it exactly what are u trying to reproduce
Zytan said:Given the above code or the exception, I take the exception any day.
At least I know for 100% certainty that it will work, and it doesn't
rely on me understanding or typing the code example correctly.
Zytan
Hmmm.... Didn't you notice that this was a reply to Arne, and that I was correcting his
sample?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.