事件处理程序委托是否可以移除自身

本文关键字:程序 是否 事件处理 | 更新日期: 2023-09-27 18:28:10

事件处理程序委托如何移除自身??

像这样的代码

    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);
  }

事件处理程序委托是否可以移除自身

你想做这样的事情吗?

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
}