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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My Service stops instantly with the error...

Status
Not open for further replies.

jwarmuth

IS-IT--Management
Sep 22, 2001
151
CA
The [(my)service name] service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example: the Performance Logs and Alerts service."

This happens when you try to start the service after installation.

Googling this error returns results not in relation to coding, but in relation to general event log errors.

My service is simple, all it does is start a counter, which triggers an EventHandler that creates an eventlog entry on cue.

I'm just looking for a general understanding of what that error means. My service has something to do, so why the error?!

If you need the source code, let me know I'd be happy to post it.

Jeff W.
MCSE, CNE
 
Post the source code. Errors & uncaught exceptions are a common cause for the specified behaviour.
 
Code:
	public class Service1 : System.ServiceProcess.ServiceBase
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string strELSource = "EventLogTheft";
		private string strELLogName = "EventLogTestApp";
		private string strELComputerName = ".";
		private int iCounter = 0;
		private Timer tmr5Seconds = null;


		public Service1()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();
			try
			{
				System.Timers.Timer tmr5Seconds = new System.Timers.Timer(1000);
				tmr5Seconds.Elapsed+= new ElapsedEventHandler(tmr5Seconds_Elapsed);
				if(!EventLog.SourceExists(strELSource, strELComputerName))
				{
					Debugger.Break();
					Console.WriteLine("Source does not exist. I'll try to create");					
					//EventLog.CreateEventSource(strELSource, strELLogName, strELComputerName);
				}					
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}

		}

		// 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);
		}

		/// <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)
		{
			if (EventLog.SourceExists(strELSource, strELComputerName))
			{
				tmr5Seconds.Enabled = true;
			}
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			tmr5Seconds.Stop();
		}

		private void tmr5Seconds_Elapsed(object sender, ElapsedEventArgs e)
		{
			try
			{
				iCounter+=1;
				EventLog el1 = new EventLog();
				el1.Source = strELSource;
				el1.Log = strELLogName;
				el1.WriteEntry("Timed event. Second: " + iCounter.ToString());
				Console.WriteLine(iCounter.ToString());
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
	}

Jeff W.
MCSE, CNE
 
In this line:
Code:
System.Timers.Timer tmr5Seconds = new System.Timers.Timer(1000);
you're telling it to create a one-shot timer -- one that will fire once, and never again. Use the timer from the [tt]System.Threading[/tt] namespace instead, where you're allowed to pass another parameter to the constructor that tells it to fire at a certain interval.
Code:
System.Threading.Timer tmr5Seconds = new Timer(MyTimerCallback, null, 1000, 5000);

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks, I'll try that as soon as I can get some time. :D

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

Part and Inventory Search

Sponsor

Back
Top