What should I use Sleep or Timer

前端 未结 3 1716
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 11:51

I have two alternative using timer or using sleep, I need to call a method every 3 seconds after this method is finished, I wrote basic example to demonstrate what I mean:

3条回答
  •  不知归路
    2021-01-18 12:15

    The real context of your program matters too.

    The sleep option 'wastes' a Thread, not a problem in a small console app but in general not a good idea.

    You don't need to restart the timer, the following will keep ticking:

        static void Main(string[] args)
        {
            var t = new System.Timers.Timer(1000);
            t.Elapsed += (s, e) => CallMeBack();
            t.Start();
    
            Console.ReadLine();
        }
    

提交回复
热议问题