class B
{
public delegate void CallBack();
public event CallBacl cllBack;
void Publish()
{
cllBack();
}
}
class A
{
B b = new B();
b.cllBack += new CallBack(test_this)
public void test_this()
{
}
}
if (cllBack != null) cllBack();
one caveat that it cost me some pain:
b.cllBack += new CallBack(test_this)
of course adds a reference to b in a, so even if you do not use b anymore in any other place, b is still referenced and will not be garbage collected.
what i think is a neater approach:
class A
{
private delegate B.CallBack myCallBack;
private B b = new B();
public A
{
myCallBack = new B.CallBack( test_this );
b.cllBack += myCallBack;
}
public void Close()
{
c.cllBack -= myCallBack;
}
}
In short you register when you need the callback, and unregister when you no longer need the callback.
hth
Mario
PS: be careful, your b is local to the ctor
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.