Pauses between commands in simple console C# app

2019-07-24 19:04发布

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

2条回答
2楼-- · 2019-07-24 19:45

use System.Diagnostics Stopwatch class in a loop

查看更多
爷、活的狠高调
3楼-- · 2019-07-24 19:48

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";
    }
}
查看更多
登录 后发表回答