I'm trying to figure out how to write a Windows service that performs a task on a set interval, however I'm having really poor luck finding a tutorial that actually works for me.
I assebled this little app from two tutorials, and despite my best efforts, it will not run properly. It installs okay, and will run without errors, however it does not seem to fire the timer event; ever. I just can't figure out why, I've simplified the application as much as possible and I've used timers before with zero problem.
Explaination of the app: every five seconds it's supposed to create an event log entry noting the time the event fired.
Here's teh code:
Jeff W.
MCSE, CNE
I assebled this little app from two tutorials, and despite my best efforts, it will not run properly. It installs okay, and will run without errors, however it does not seem to fire the timer event; ever. I just can't figure out why, I've simplified the application as much as possible and I've used timers before with zero problem.
Explaination of the app: every five seconds it's supposed to create an event log entry noting the time the event fired.
Here's teh code:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;
using System.IO;
namespace Learn2ServiceToEventLog
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
static System.Timers.Timer aTimer;
public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
// System.Timers.Timer aTimer = new System.Timers.Timer(5000);
// aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
Service1.aTimer = new System.Timers.Timer(5000);
Service1.aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
Service1.aTimer.Enabled = true;
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
Service1.aTimer.Enabled = false;
}
private static void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
//test for ElapsedEvent
EventLogTraceListener logTrace = new EventLogTraceListener("ServiceCodeTestingApp");
Trace.Listeners.Add(logTrace);
string strOutput = "Executed at: " + DateTime.Now.TimeOfDay.ToString();
logTrace.WriteLine("Test Service App", strOutput );
logTrace.Flush();
}
}
}
Jeff W.
MCSE, CNE