C# - anonymous functions and event handlers

后端 未结 4 592
情深已故
情深已故 2021-02-02 10:50

I have the following code:

public List FindStepsByType(IWFResource res)  
{  
    List retval = new List&         


        
4条回答
  •  -上瘾入骨i
    2021-02-02 11:13

    Below is approach about how unsubscribe event in anonymous method:

    DispatcherTimer _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMilliseconds(1000);
    EventHandler handler = null;
    
    int i = 0;
    
    _timer.Tick += handler = new EventHandler(delegate(object s, EventArgs ev)
    {
        i++;
        if(i==10)
            _timer.Tick -= handler;
    });
    
    _timer.Start();
    

提交回复
热议问题