awaiting Task.Delay makes this operation quite easy, in combination with File.ReadLines:public async Task SlowPrint(string fileName){ //TODO stuff to generate real file path and check if it exists foreach(var line in File.ReadLines(fileName)) { Console.WriteLine(line); await Task.Delay(700); }} C#5.0之前的解决方案较难,但肯定可以实现.只需创建一个计时器并在每次触发时读取新行即可:A pre C# 5.0 solution is harder, but certainly possible. Just create a timer and read a new line whenever it fires:public void SlowPrint(string FileName){ var iterator = File.ReadLines(FileName).GetEnumerator(); System.Threading.Timer timer = null; timer = new System.Threading.Timer(o => { if (iterator.MoveNext()) Console.WriteLine(iterator.Current); else { iterator.Dispose(); timer.Dispose(); } }, null, 700, Timeout.Infinite);} 这篇关于使用计时器延迟逐行readline方法C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 04:45
查看更多