提问人:Lnio Yarschov 提问时间:9/3/2023 更新时间:9/3/2023 访问量:29
C# 事件和委托 - 如何延迟调用事件的订阅者?
C# Events and Delegates - How do I delay an event's subscriber from being called?
问:
我了解了事件和委托,这里有一个简单的例子:
public class Subject {
public delegate void RandomDelegate();
public event RandomDelegate randomEvent;
void CallRandom() {
if (randomEvent != null) {randomEvent();}
}
}
我可以,每当调用randomEvent时进行处理。但是,我如何处理延迟事件处理程序,该处理程序在订阅后调用第n次randomEvent,并在之后删除自身?
我试着做一个类来做这件事,但我很困惑。Subject.randomEvent += randomEventHandler;
public class DelayedHandler
{
public int CurrentDelay { get; private set; } = 0;
public int Delay { get; init; }
public DelayedHandler(int delay)
{
Delay = delay;
}
public void Handler() // randomEvent += new DelayedHandler().Handler
{
if (CurrentDelay >= Delay)
{
// Logic
}
else
{
CurrentDelay++;
}
}
}
答: 暂无答案
评论