Pauses between commands in simple console C# app

感情迁移 提交于 2019-12-12 18:19:20

问题


Is there any easy how to e.g. have 1 second delay between commands? I can only think of some large cycles. I know about thread.sleep but it is not what I am looking for - I would like to just use something else without relation to threads. Thanks for any idea


回答1:


I think that you are after something like yield return.

This allows you to have the pass control back to the calling code and then can carry on through your app. The link has a good example of its use.

using System;
using System.Collections;

class Test
{
    static void Main(string[] args)
    {
        foreach (string x in Foo())
        {
            Console.WriteLine (x);
        }
    }

    static IEnumerable Foo()
    {
        yield return "Hello";
        yield return "there";
    }
}



回答2:


use System.Diagnostics Stopwatch class in a loop



来源:https://stackoverflow.com/questions/3913182/pauses-between-commands-in-simple-console-c-sharp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!