Ramesh Balasubramani Ответов: 2

Лучший способ вызывать метод каждую секунду в службе windows


Best way to call method every second in windows service


Продвинутая Благодарность !!!

Что я уже пробовал:

<pre>
//This one triggering the MyMethod(); 8 times after that it not triggering MyMethod();
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(1);

var timer = new System.Threading.Timer((e) =>
{
    MyMethod();   
}, null, startTimeSpan, periodTimeSpan);

CHill60

Что плохого в том, что у вас есть? (кроме того, что вы собираетесь связать процессор)

2 Ответов

Рейтинг:
6

Ramesh Balasubramani

Task task = new Task(() =>
                {
                    while (true)
                    {
                        MyMethod();
                        Thread.Sleep(1000);
                    }
                });
                task.Start();


Рейтинг:
16

Mehdi Gholam

Я обычно использую :

System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.AutoReset = true; // the key is here so it repeats
timer.Elapsed += timer_elapsed;
timer.Start();