我有一个类(Class A
),负责运行的,看起来像这样的背景下异步工作:使用事件唤醒异步长时间运行工作
public async void DoJob()
{
while (true)
{
var thingToDo = this.getNextThing();
if (thingToDo != null)
{
try
{
await this.performAction(thingToDo);
}
catch (Exception ex)
{
// file logging of error.
// then wait a certain period.
await Task.Delay(someInterval);
}
}
else
{
// Gets the interval that should be awaited until there is a
// thingToDo available.
var waitInterval = this.getWaitUntilNextThingAvailable();
// if there such an interval then wait for it.
if (waitInterval != null)
{
await Task.Delay(waitInterval.Value);
}
// else (basically when there is nothing to be done by this job)
// use an AsyncManualResetEvent to wait until its set.
else
{
await this.waitHandle.WaitAsync();
}
}
}
}
我在最后else
基本上感兴趣块 - 其中我使用AsyncManualResetEvent
(由AsyncEx库提供)我使用由另一个类(Class B
)提供的事件来设置waitHandle
所述一个
。这是预订的样子(注意,此方法是在Class B
)
private event Action ChangeOccurred;
public void Attach(Action action)
{
this.ChangeOccurred += action;
}
现在到我的问题:我用ChangeOccurred?.Invoke()
设置waitHandle
这样Class A
可以被通知,有事可做,并继续在后台执行任务。
是Invoke()
正确的方法?我不确定是否应该使用BeginInvoke()
和EndInvoke
代替?该事件不包含日期,仅用作异步作业已可以执行操作的signal
。
Class B
其中ChangeOccurred
事件被调用的代码是同步的。