How to find out that an object has disposed of?

I have a multi-threaded application, and the CancellationToken used as a shared object. Each thread can initiate it to inform other threads that the job has been canceled. Then, one thread performs a cleanup and allocates each object similar to this CancellationToken . Then, if the thread tries to use it, an exception is thrown:

Canceled file CancellationTokenSource.

How can I find out that an object is before using it?

+6
source share
4 answers

Well, according to Reflector, CancellationTokenSource has an internal IsDisposed method that could tell you, but since it's internal, you shouldn't call it.

In any case, if one thread destroys data structures and objects that other threads depend on, then do not do this. Correct your code and leave these objects alive for the time they are needed.

In other words, wait for these other threads to complete by requiring a CancellationTokenSource before getting rid of it.

+4
source

The correct course of action will be that the creators of some disposable objects slightly deviate from the Microsoft rule that performs any action on the remote object, should throw an exception and instead follow the more general rule that the exception should be thrown at any time when the post -conditions of the method cannot be met. If the purpose of the Cancel method is to ensure that no one continues to view the work as live, and even before the Cancel method is called, everyone considers the work dead, then the post-condition for the method is fulfilled regardless of whether it is located whether the object.

Typically, code outside of a well-designed object should not ask if it has been deleted, except to possibly state that it was. Instead, the object itself must provide methods whose value on the placed object will be clear and unambiguous. These methods may internally use the IsDisposed flag, but it will need to use any lock necessary to prevent race conditions. In general, a template

  if (! myThing.isDisposed) 
     myThing.DoSomething ();

is a sign that myThing should really support the DoSomethingIfNotDisposed method (possibly called TryDoSomething). If you can't do this, my tendency might be to write your own DoSomethingIfNotDisposed extension method and use Try / Catch to drown out ObjectDisposedException (or any specific exception that the object should throw).

+2
source

check if the object is before using it.

Still not the best design. however, here is what I use to determine if an object is located.

 if (!object.IsDisposed) object.DoSomething(); 

or

 public string DoSomething() { if (this.IsDisposed) return null; } 

If this does not work, you can try adding the IsDisposed flag and overriding the dispose method. And set this to true in your own code.

+1
source

Inherit your class and add a property:

 class MyCancellationTokenSource: CancellationTokenSource { public bool MyIsDisposed { get; private set; } protected override void Dispose(bool disposing) { base.Dispose(disposing); MyIsDisposed = true; } } 
0
source

Source: https://habr.com/ru/post/885859/


All Articles