Liviu Trifoi
Liviu Trifoi

Reputation: 3030

WeakEvent garbage collection in C#

I'm thinking today at the following scenario:

I have two classes A and B.

A exposes an event E.

B subscribes to this event "E", using a weak event handler "W".

After a while nobody holds any reference to B, but the GC has not yet kicked in.

While B is not yet collected by the GC and A raises "E", will the weak event handler be called?

I'm thinking yes, but I'm curios if I'm not missing something.

Upvotes: 4

Views: 277

Answers (1)

Chris Shain
Chris Shain

Reputation: 51329

Yes it will. The WeakEventManager will see that the object B references is 'alive' (not yet collected) and will raise the event there.

Upvotes: 2

Related Questions