waking the service every five minutes  
Author Message
Sarguna





PostPosted: Visual C# General, waking the service every five minutes Top

hello Everybody

i have made a windows service in C#

I want my service to wake up every 5 min and do some processing


how can it be done
It is urgent

Please mail some raw code if possible




Visual C#13  
 
 
SteveDrake





PostPosted: Visual C# General, waking the service every five minutes Top

Hello,

You have a few options.

This is the code for the simple component based timer :

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace ConsoleApplication1
{
class Program
{
static Timer timer;
static void Main(string[] args)
{
timer = new Timer(5 * 60); // 5 Mins
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
System.Console.WriteLine("Press any key to exit");
System.Console.ReadKey();
}

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
System.Console.WriteLine("Beep");
}
}
}

And now with the Thread Timer class (note the Using Change ) :

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
static Timer timer;
static void Main(string[] args)
{
timer = new Timer( Beeper, null, new TimeSpan(0), new TimeSpan(0, 5, 0));

System.Console.WriteLine("Press any key to exit");
System.Console.ReadKey();
}
static void Beeper(object state)
{
System.Console.WriteLine("Beep");

}

}
}

You can add code to the service pause to pause and resume the timers. Hope this help.