how to have a function run inside a service every 10 minutes?

后端 未结 5 988
时光取名叫无心
时光取名叫无心 2021-02-01 11:16

I have a windows service running, inside this i want to run a function every then minutes. I have found some code but it doesn\'t seem to work? I have a logger and it does not s

5条回答
  •  面向向阳花
    2021-02-01 11:27

    I need this functionality also. That is, my C# windows service must check email every 10 minutes. I stripped down some logic to make the code more effective, as follows :

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                _timer.Stop();
                try
                {
                    EventLog.WriteEntry(Program.EventLogName, "Checking emails " + _count++);
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry(Program.EventLogName, "This is my error " + ex.Message);
                }
                _timer.Start();
            }
    

    The timer_elapsed method indeed will be call every 10 minutes, starting from the first _timer.start(), which you miss it by the way. I haven't done any checking of the _lastRun and startAt. I don't think we need it

提交回复
热议问题