Is there a Task based replacement for System.Threading.Timer?

前端 未结 7 970
日久生厌
日久生厌 2020-11-27 09:49

I\'m new to .Net 4.0\'s Tasks and I wasn\'t able to find what I thought would be a Task based replacement or implementation of a Timer, e.g. a periodic Task. Is there such a

7条回答
  •  眼角桃花
    2020-11-27 10:05

    I ran into a similar problem and wrote a TaskTimer class that returns a seris of tasks that complete on timer: https://github.com/ikriv/tasktimer/.

    using (var timer = new TaskTimer(1000).Start())
    {
        // Call DoStuff() every second
        foreach (var task in timer)
        {
            await task;
            DoStuff();
        }
    }
    

提交回复
热议问题