我想用一种简单的异步方法:
async Task Loop(int timeInMs, int iterationTimeInMs)
{
for (int current = 0; current < timeInMs; current += iterationTimeInMs)
{
await Task.Delay(iterationTimeInMs);
var normalizedTime = current / (double)timeInMs;
someValue = Lerp(from, to, normalizedTime);
}
}
如果你的
Lerp
可能需要很长时间
async Task Loop(int timeInMs, int iterationTimeInMs)
{
Task taskFromLastIteration = null;
for (int current = 0; current < timeInMs; current += iterationTimeInMs);
{
var delay = Task.Delay(iterationTimeInMs);
if (taskFromLastIteration != null)
someValue = await taskFromLastIteration;
await delay;
var normalizedTime = current / (double)timeInMs;
taskFromLastIteration = Task.Run(() => Lerp(from, to, normalizedTime));
}
if (taskFromLastIteration != null)
someValue = await taskFromLastIteration;
}