public partial class MyService : ServiceBase
{
private System.Timers.Timer timer2;
private bool IsProcessRunning = false;
public MyService()
{
InitializeComponent();
this.ServiceName = "MyService";
timer2 = new System.Timers.Timer();
timer2.Enabled = false;
timer2.Interval = "10000";
timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
}
protected override void OnStart(string[] args)
{
timer2.Enabled = true;
timer2.Start();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!IsProcessRunning)
{
try
{
IsProcessRunning = true;
// Add process here
InsertRecord();
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 1000);
}
finally
{
IsProcessRunning = false;
}
}
}
}
Blogs on .NET and LAMP Technologies
There's often more than one correct thing.
There's often more than one right thing.
There's often more than one obvious thing.
--Larry Wall
Thursday, October 04, 2007
Implementing timer operation in window service
Following code snippet shows how can we perform a operation on timer event in window service:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment