I'm working on a ReloadableCollection that reloads once per second on a background thread inside of it. The problem is that when I nullify an instance of the collection, the thread doesn't stop (because it doesn't know that the instance in which it is working has been nullified).
I have tried multiple things, like using a wrapper for the instance, making ThreadWork method static and providing it the instance at startup (start(this)), setting cancel to false in the destructor of the Collection, ... nothing worked.
An example of the problem can be seen in the code bellow.
My collection class:
class Collection
{
private const int INTERVAL=1000;
Thread thread;
public Collection()
{
thread=new Thread(ThreadWork);
thread.Start();
}
private void ThreadWork()
{
while(true){ // how to detect when the instance is nullified?
Reload();
Thread.Sleep(INTERVAL);
}
}
private void Reload()
{
// reload the items if there are any changes
}
}
Example usage:
void Main()
{
Collection myCollection=new Collection();
// ...
// here, it is reloading, like it should be
// ...
myCollection=null;
// ...
// here, it should not be reloading anymore, but the thread is still running and therefore "reloading"
}