Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EventLapsed timer method not executing in a Service

Status
Not open for further replies.

jwarmuth

IS-IT--Management
Sep 22, 2001
151
CA
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:

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
 
Well, you're communicating between multiple threads and now you're using static methods. this can get complicated. have you considered using the sytem.windows.forms.timer instead of the threading timer?

You are also playing with services - which is a whole new world of permissions and roles.
 
I can't use the forms.timer object because it won't run in a service.

As for permissions, it successfully installs and runs under several different accounts; Local System, Network System, and my user account with has administrative access.

I played with the threading.timer object before and had moderate success, however I came across a tutorial demonstating the timers.timer object in a windows service as a way to fire events at specified intervals so I'm trying to figure it out.

Here's a link to the tutorial I'm talking about.

Jeff W.
MCSE, CNE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top