Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out an object has disposed?

I have a multi-threaded application and a CancellationToken is used as a shared object. Every thread can trigger it to tell the other threads the job is cancelled. Then one thread does the clean-up and disposes every object like this CancellationToken. Then if a thread tries to use it, an exception is raised:

The CancellationTokenSource has been disposed.

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

like image 575
Xaqron Avatar asked Apr 13 '11 20:04

Xaqron


1 Answers

Well, according to Reflector, CancellationTokenSource has an internal IsDisposed method that could've told you, but since it's internal, you're not supposed to call it.

In any case, if one thread yanks out data structures and objects other threads depend on, then don't do that. Fix your code and leave those objects live for the duration of their need.

In other words, wait for those other threads to finish needing the CancellationTokenSource before you dispose of it.

like image 90
Lasse V. Karlsen Avatar answered Sep 20 '22 08:09

Lasse V. Karlsen