事件处理程序委托可以自行删除

时间:2013-08-19 18:35:00

标签: c# event-handling

如何在事件处理程序委托中删除自己??

像这样的代码

    void timerTick(object sender, EventArgs e)
    {
        if (!IsHolding){
            return;
        }

        UtilityStoryboardManager.PlayerStoryboard("End", (_) =>{
            IsHolding = false;
            //call some function or  perform some logic
            timer.Stop();
            //How Can a eventhandler delegate remove itself ??
            //timer.Tick -= timerTick;
        }, null);
  }

1 个答案:

答案 0 :(得分:0)

你想做这样的事吗?

void timerTick(object sender, EventArgs e)
{
    if (!IsHolding){
        return;
    }

    UtilityStoryboardManager.PlayerStoryboard("End", (_) =>{
        IsHolding = false;
        //call some function or  perform some logic
        timer.Stop();
        //How Can a eventhandler delegate remove itself ??
        //timer.Tick -= timerTick;
    }, null);
 (sender as Timer).Tick-=timerTick; // removes the event
}