我想通知用户完成操作的估计时间。长操作发生在如下所示的序列中:
var processedItems = items.Select(x => Process(x));
每个Process(x)
调用可能需要几秒钟才能完成。
我想知道一种简单干净的方法来动态估计剩余的可枚举投影。
也许使用System.Reactive
?
首先,这不可能用IEnumerable<T>
来做,因为没有办法获得元素的数量。为此,您应该使用任何实现ICollection<T>
的东西,这样您就可以获得项目数。
其次,你不能真正使用现有的Select
方法(并非没有一些黑客攻击(,但你可以编写自己的方法。这是我敲出的东西,它将在投影期间为列表中的每个元素调用操作。
首先是一堂课来保存当前进度的详细信息。
public class SelectProgress
{
public decimal Percentage { get; set; }
public TimeSpan TimeTaken { get; set; }
public TimeSpan EstimatedTotalTime { get; set; }
}
和自定义Select
方法:
public static IEnumerable<TResult> Select<TSource, TResult>(
this ICollection<TSource> source,
Func<TSource, TResult> selector,
Action<SelectProgress> timeRemaining)
{
Stopwatch timer = new Stopwatch();
timer.Start();
var counter = 0;
foreach (var element in source)
{
yield return selector(element);
counter++;
timeRemaining?.Invoke(new SelectProgress
{
Percentage = counter/(decimal)source.Count,
TimeTaken = timer.Elapsed,
EstimatedTotalTime =
TimeSpan.FromTicks(timer.Elapsed.Ticks/counter * source.Count)
});
}
}
并像这样称呼它:
//Let's have a list of numbers to play with
var list = Enumerable.Range(1, 20).ToList();
var results = list.Select(
i =>
{
//Just an artificial delay
Thread.Sleep(1000);
//Return the string representation of the number, you know,
//just something fun to do here really
return i.ToString();
},
//Just going to output the values here, but you can choose to do whatever you like
p => Console.WriteLine(
$"{p.Percentage:P2}: Taken: {p.TimeTaken}, Total: {p.EstimatedTotalTime}"))
.ToList();
此代码将生成如下所示的输出:
5.00%: Time taken: 00:00:01.0007261, Estimated total: 00:00:20.0158420
10.00%: Time taken: 00:00:02.0015503, Estimated total: 00:00:20.0155100
15.00%: Time taken: 00:00:03.0017421, Estimated total: 00:00:20.0116180
<snip>
90.00%: Time taken: 00:00:18.0101580, Estimated total: 00:00:20.0112860
95.00%: Time taken: 00:00:19.0103062, Estimated total: 00:00:20.0108480
100.00%: Time taken: 00:00:20.0107314, Estimated total: 00:00:20.0107320