Pauses between commands in simple console C# app

2019-07-24 19:09发布

问题:

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