Search code examples
c#windows-services

System.Windows.Forms.Timer in windows server


Could someone please provide me with an example on how to use the System.Windows.Forms.Timer in a windows service. The timer should 'invoke' a non-static windows service method every 10 seconds. I have (unsuccessfully) tried this in my OnStart method:

_timer.Tick += new EventHandler(StartProcessingItems);
_timer.Interval = 10000;
_timer.Enabled = true;
_timer.Start();    

Here StartProcessingItems is my on-static windows service method.


Solution

  • use instead System.Threading.Timer:

    var t = new Timer(o => 
    {
       Console.WriteLine("Hello from the past! " + (DateTime)o);
    }, DateTime.Now, TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1));
    

    It will execute the delegate 10s after, and then with a frequency of 1min

    http://msdn.microsoft.com/fr-fr/library/system.threading.timer%28v=vs.80%29.aspx