ActionBlock测试方法永远不会完成

时间:2017-11-26 11:37:15

标签: c# tpl-dataflow

我有以下小测试方法来玩ActionBlock。它以预期的顺序写出跟踪消息,但它永远不会完成。测试方法永远不会退出。

我在这里做错了什么? Thx提前。

[TestMethod]
public async Task ActionBlockTest()
{
    var rnd = new Random(DateTime.Now.Millisecond);
    var delay = new ActionBlock<int>(item =>
    {
        var start = DateTime.Now;
        Trace.WriteLine(string.Format("{0:ss fff} item start processing ({1})", start, item));

        return Task.Delay(item).ContinueWith(t => Trace.WriteLine(string.Format("{0:ss fff} item finished processing ({1}) took {2} ms", DateTime.Now, item, (DateTime.Now - start).TotalMilliseconds)));
    }, new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 4,
        MaxDegreeOfParallelism = 2
    });

    for(int i = 0; i < 8; i++)
    {
        var d = rnd.Next(100, 1000);
        Trace.WriteLine(string.Format("{0:ss fff} posting {1}", DateTime.Now, d));
        await delay.SendAsync(d);
    }

    await delay.Completion;
}

0 个答案:

没有答案