المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to implement a timer in Windows Service correctly?



C# Programming
07-15-2009, 07:20 PM
In this windows service, I have a log configured to record the Windows Service Start and Stop state and the process triggered by a timer. The process is sending an email notification once a day.

Currently, the Windows Service Start and Stop state is recoding successfully in the log. The email notification process triggered by the timer however is not doing anything. Any help is much appreciated.

See my script below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;

namespace IRISEmailNotifier
{
public partial class IRISEmailNotifier : ServiceBase
{
public IRISEmailNotifier()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
try
{
LogEntry le = new LogEntry("Start - " + DateTime.Now.ToString() + "\n");

this.timer1.Interval = 10000;
this.timer1.Enabled = true;
this.timer1.Tick += new EventHandler(timer1_Tick);
}
catch (Exception ex)
{
LogEntry le = new LogEntry(ex.ToString());
}
}

void timer1_Tick(object sender, EventArgs e)
{
try
{
LogEntry le = new LogEntry("Send Email - " + DateTime.Now.ToString() + "\n");
}
catch (Exception ex)
{
LogEntry le = new LogEntry(ex.ToString());
}
}

protected override void OnStop()
{
try
{
LogEntry le = new LogEntry("Stop - " + DateTime.Now.ToString() + "\n");

this.timer1.Enabled = false;
}
catch (Exception ex)
{
LogEntry le = new LogEntry(ex.ToString());
}
}

private void SendEmailNotification()
{
try
{
LogEntry le = new LogEntry("Send Email - " + DateTime.Now.ToString() + "\n");
}
catch (Exception ex)
{
LogEntry le = new LogEntry(ex.ToString());
}
}
}
}

Thanks, Steve C.

Steve C.
kstv@netzero.net