Garbage collection and Threads.

M

MaSTeR

Hello,

I have an object that at some point creates a thread to run one of its
method.

I create that object within an application and after a while I set its
reference to null to let the gc reclaim it. now the problem is that I want
this thread to stop after the object is being deleted but this object sits
around because its main thread and the one it creates reference each other
even though me, from my application I have not reference of those objects.
Now calling dispose it's not an option because those object get created via
a COM and I can't change that to call an Unload method or whatever.

Any ideas ?

Thanks everyone.
 
N

Nicholas Paldino [.NET/C# MVP]

You say that the main thread and the worker thread reference each other,
but you also say that you release the references? Only one of those
statements can be true. Either you have a reference to it or you don't?

However, this doesn't mean that the thread stops running.

Is it a COM object that is creating the thread? If so, why do you want
to stop it? The COM object should be responsible for terminating the thread
gracefully, especially if you have released it.

If your object has a thread, and you have implemented IDispose on it,
then you should stop the thread when the Dispose method is called. If you
don't have an implementation of IDispose on this method, then you should.

Hope this helps.
 
M

MaSTeR

Nicholas Paldino said:
You say that the main thread and the worker thread reference each other,
but you also say that you release the references? Only one of those
statements can be true. Either you have a reference to it or you don't?
Sorry but again both statements are true. I have an object creating the
other object (so my first object has a reference to it). The second object
creates a thread that by default has a reference to the main thread (new
Thread(new StartThread(this.method)).

Now my first object sets to null the reference to the second object that
will never be garbage collected because it has a thread running. In C++ the
main object (the COM object) would have called Release-> (as it does now,
I've made my .Net object a com object) and this would have called the
destructor where you could handle thread signaling. With .Net objects this
doesn't happen.
However, this doesn't mean that the thread stops running.
Exactly.

Is it a COM object that is creating the thread? If so, why do you want
to stop it? The COM object should be responsible for terminating the thread
gracefully, especially if you have released it.
The com object is only responsible of calling Release on the COM object it
creates, doesn't matter if they're C++, VB or .Net.
If your object has a thread, and you have implemented IDispose on it,
then you should stop the thread when the Dispose method is called. If you
don't have an implementation of IDispose on this method, then you should.
As I said adding a method call from the main com object on termination is
not an option.
Hope this helps.
thanks for your help !
 

Ask a Question

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.

Ask a Question

Top