我在 .NET 4.7.1 中使用 DataFlow 库由于某种原因,当我await Console.Out.WriteLineAsync(DateTime.Now.TimeOfDay.ToString());在构造函数的asynclambda 中时,我的程序永远不会终止。ActionBlock它只会输出行的流DateTime.Now.TimeOfDay.ToString()并随机停止,永远不会到达Console.WriteLine("Time elapsed:" + watch.Elapsed);,尽管在某些情况下我观察到控制台输出"Finished Reading the file"。
class Program
{
public static async Task Main(string[] args)
{
int numberOfLines = 0;
Console.WriteLine("Number of cores used:" + Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 2.0)));
BufferBlock<string> queBuffer = new BufferBlock<string>(new DataflowBlockOptions { BoundedCapacity = 100000 });
var processingBlock = new ActionBlock<string>(async inputline =>
{
Interlocked.Increment(ref numberOfLines);
//Line that causes issue
//await Console.Out.WriteLineAsync(DateTime.Now.TimeOfDay.ToString());
}
, new ExecutionDataflowBlockOptions()
{
MaxDegreeOfParallelism = 48,
SingleProducerConstrained = true,
BoundedCapacity = 500
});
queBuffer.LinkTo(processingBlock);
//Start
var watch = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("Processing started at:" + DateTime.Now);
if (File.Exists(args[0]))
{
using (StreamReader sr = new StreamReader(args[0]))
{
}
}
但是,如果我取出导致问题的行,它会工作并从文本文件中读取所有行。 W:\test>.\CompressAMS.exe token2-small.txt
Number of cores used:24
Processing started at:12/17/2018 6:32:50 PM
Finished Reading the file
Time elapsed:00:00:00.3569824
Number of lines read:100000
LEATH
相关分类