What should I use Sleep or Timer

前端 未结 3 1698
佛祖请我去吃肉
佛祖请我去吃肉 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:19

    Do you realize that fooUsingSleep is calling itself over and over? It will eventually generate a stack overflow.

    If you are using timer, it can be as simple as this:

        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        t.Interval = 3000;
        t.Tick += new EventHandler((o,ea) => Console.WriteLine("foo"));
    

提交回复
热议问题