我想将多个事件简化为单个延迟操作。在发生一些触发后,我预计会出现更多类似的触发器,但我不想重复产生的延迟动作。行动等待,以便有机会完成爆发
问题:如何以优雅的可重复使用方式进行?
直到现在我使用了一个属性来标记事件并触发延迟操作,如下所示:
public void SomeMethod()
{
SomeFlag = true; //this will intentionally return to the caller before completing the resulting buffered actions.
}
private bool someFlag;
public bool SomeFlag
{
get { return someFlag; }
set
{
if (someFlag != value)
{
someFlag = value;
if (value)
SomeDelayedMethod(5000);
}
}
}
public async void SomeDelayedMethod(int delay)
{
//some bufferred work.
await Task.Delay(delay);
SomeFlag = false;
}
下面是一个较短的方法,但仍然不是通用的或可重用的...我想要一些简洁的包装动作和标志,并保持功能(在执行完成之前返回调用者(如今))。 我还需要能够将对象引用传递给此操作)
public void SerializeAccountsToConfig()
{
if (!alreadyFlagged)
{
alreadyFlagged = true;
SerializeDelayed(5000, Serialize);
}
}
public async void SerializeDelayed(int delay, Action whatToDo)
{
await Task.Delay(delay);
whatToDo();
}
private bool alreadyFlagged;
private void Serialize()
{
//some buferred work.
//string json = JsonConvert.SerializeObject(Accounts, Formatting.Indented);
//Settings1.Default.Accounts = json;
//Settings1.Default.Save();
alreadyFlagged = false;
}
答案 0 :(得分:4)
这是一个线程安全且可重复使用的解决方案。
您可以创建DelayedSingleAction
的实例,并在构造函数中传递您想要执行的操作。我相信这是线程安全的,尽管在启动操作之前它会重新启动计时器的风险很小,但我认为无论解决方案是什么,风险都会存在。
public class DelayedSingleAction
{
private readonly Action _action;
private readonly long _millisecondsDelay;
private long _syncValue = 1;
public DelayedSingleAction(Action action, long millisecondsDelay)
{
_action = action;
_millisecondsDelay = millisecondsDelay;
}
private Task _waitingTask = null;
private void DoActionAndClearTask(Task _)
{
Interlocked.Exchange(ref _syncValue, 1);
_action();
}
public void PerformAction()
{
if (Interlocked.Exchange(ref _syncValue, 0) == 1)
{
_waitingTask = Task.Delay(TimeSpan.FromMilliseconds(_millisecondsDelay))
.ContinueWith(DoActionAndClearTask);
}
}
public Task Complete()
{
return _waitingTask ?? Task.FromResult(0);
}
}
请参阅此dotnetfiddle以获取从多个线程连续调用一个操作的示例。
答案 1 :(得分:2)
由于您对RX这里的简单控制台应用示例感兴趣:
javac -sourcepath . path/to/Main.java
答案 2 :(得分:0)
基于Andrew提出的解决方案,这是一个更通用的解决方案 延迟行动的声明和实例创建:
public DelayedSingleAction<Account> SendMailD;
在函数内或构造函数中创建实例(这可以是每个处理不同对象的此类操作的集合):
SendMailD = new DelayedSingleAction<Account>(SendMail, AccountRef, 5000);
反复调用此操作
SendMailD.PerformAction();
发送邮件是您将要执行的操作&#34;爆发控制&#34;。它的签名匹配:
public int SendMail(Account A)
{}
这是更新的课程
public class DelayedSingleAction<T>
{
private readonly Func<T, int> actionOnObj;
private T tInstance;
private readonly long millisecondsDelay;
private long _syncValue = 1;
public DelayedSingleAction(Func<T, int> ActionOnObj, T TInstance, long MillisecondsDelay)
{
actionOnObj = ActionOnObj;
tInstance = TInstance;
millisecondsDelay = MillisecondsDelay;
}
private Task _waitingTask = null;
private void DoActionAndClearTask(Task _)
{
Console.WriteLine(string.Format("{0:h:mm:ss.fff} DelayedSingleAction Resetting SyncObject: Thread {1} for {2}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, tInstance));
Interlocked.Exchange(ref _syncValue, 1);
actionOnObj(tInstance);
}
public void PerformAction()
{
if (Interlocked.Exchange(ref _syncValue, 0) == 1)
{
Console.WriteLine(string.Format("{0:h:mm:ss.fff} DelayedSingleAction Starting the timer: Thread {1} for {2}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, tInstance));
_waitingTask = Task.Delay(TimeSpan.FromMilliseconds(millisecondsDelay)).ContinueWith(DoActionAndClearTask);
}
}
public Task Complete()
{
return _waitingTask ?? Task.FromResult(0);
}
}