Self referencing timer /uncollectable object
<p>I have a type that contains a timer which fires an instance method every 2 seconds. My problem is that when the object instance drops out of scope the object is not collected because the timer thread maintains a reference. </p>

<p>My question is; Is it possible to determine, at runtime, how many references a object has and if the reference count is 1 is that reference a circular reference to the same object? Basically I was thinking of adding code to the timer call back to stop the timer and set the stack reference to null if the object only has 1 circular reference.</p>

<p>Thanks</p>

<pre><code>public class TypeWithSelfRefresh&lt;T&gt;
{
    private readonly IList&lt;T&gt; _cache = new List&lt;T&gt;();
    private Timer _refreshTimer;

    public TypeWithSelfRefresh()
    {
        _refreshTimer = new Timer(delegate { ClearCache();},null,2000,2000);
    }

    public void ClearCache()
    {
        lock (_cache)
        {
            _cache.Clear();
        }
    }

    public void Add(T item)
    {
        lock (_cache)
        {
            _cache.Add(item);
        }
    }

    public IEnumerable&lt;T&gt; GetItems()
    {
        lock (_cache)
        {
            foreach (T item in _cache)
            {
                yield return item;
            }
        }
    }
}
</code></pre>
