Deleting from parent object a child object when something happens in a
child's internal thread C#
Let's say I have ObjectA (of classA), which contains:
List<classB> bList;
now let's suppose that in each objectB there are a couple of threads which
are running (they can't be accessed from outisde objectB), threadB1 and
threadB2.
Now, in a certain objectB of such list, threadB2 discovers that
objectBRemove==true.
When that happens, I want to terminate all threads from such object and
remove it from bList (I effectively want to destroy this objectB).
I thought I could rise an event inside objectB and subscribe a objectA's
method to such event:
public void onObjectBRemove(object sender, EventArgs e)
{
bList.Remove((classB)sender);
}
When this is called after objectB's event rising this should remove
objectB from the list. Then, the garbage collector should notice objectB
becoming unreferenced and thus deleting it, also terminating all internal
threads.
Is this supposed to work? Is this a reasonable approach to the problem at
hand? Thanks.
No comments:
Post a Comment