How to Kill an Object?

F

Franklin

Suppose we have an object that is raising events - some class that we write
that has an event to which other objects in the app can register. I have
read that when a .NET event publisher is disposed, all subscribers are
automatically unregistered.

But I have also read that if subscribers are not explicitly unregistered,
then the GC won't collect the publisher - and the publisher will remain in
memory and even possibly continue to raise events.

So I wrote a small console app to test this. But I got hung up on the part
of the little console app that is supposed to "kill" or "destroy" or somehow
cause the event publisher to be disposed. But I don't know how to do that.
Setting a reference variable to the publisher to null obviously won't get
the job done. So, how can I "kill" or "destroy" the event publisher?

FWIW: I ultimatelyl want to be have a little demo app that proves one point
or another - that the subscribers are automatically unregistered from the
publisher when the publisher is disposed; or have it show that the publisher
cannot be disposed while it has any subscribers to one of its events. I
think I have the demo app ready to go, except I don't know how to make .NET
dispose [or attempt to dispose] of the publisher.

Thanks.
 
C

Cor Ligthert[MVP]

Franklin,

You cannot dispose, kill, release or whatever an object yourself.

But as there are no delegates to a method which let it run, it does not run.

Therefore simple remove those delegates.

As there are no references too or from an object anymore it will be disposed
by the GC at the best time for that.

Cor
 

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