Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Wrote a small windows service with help from guys here, but it won't stay started. It starts then stops, not creating the log file it is suppose to. It's not yet done, but no point in continuing like this.
Did I miss something somewhere?

C#
public class PortionPopUp : ServiceBase
	{
		public const string MyServiceName = "PortionPopUp";
		Thread pop_thread = null;
		
		public PortionPopUp()
		{
			InitializeComponent();
		}
		
		public static void main()
		{
			try
			{
				//Starts the PopUp Service.
				System.ServiceProcess.ServiceBase[] ServicesToRun;
				ServicesToRun = new System.ServiceProcess.ServiceBase[]
				{
					new PortionPopUp ()
				};
				ServiceBase.Run(ServicesToRun);
			}
			catch(Exception e)
			{
				// ?!?!
				Process.Start(@"%SystemRoot%\system32\calc.exe");
			}
		}
		
		private void InitializeComponent()
		{
			this.ServiceName = MyServiceName;
		}
		
		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);
		}
		
		protected override void OnStart(string[] args)
		{
			try
			{
				// Writes to log file -- see WriteToFile
				// Checks if thread is running
				WriteToFile("Starting service...");
				if(pop_thread != null && pop_thread.IsAlive)
				{
					pop_thread.Abort();
					pop_thread = null;
					//kills thread
				}
				
				pop_thread = new Thread(new ThreadStart(ThreadProc));
				pop_thread.Start();
				//assigns and starts thread
			}
			catch(Exception e)
			{
				WriteToFile("Onstart");
			}
		}
		
		protected override void OnStop()
		{
			try
			{
				WriteToFile("Stopping service...");
				pop_thread.Abort();
			}
			catch(Exception e)
			{
				WriteToFile("OnStop");
			}
		}
		
		protected void ThreadProc()
		{
			try
			{
				DateTime temp = DateTime.Now;
				DateTime nextRun = new DateTime (temp.Year, temp.Month, temp.Day, 16, 00, 0);
				while (true)
				{
					DateTime current = DateTime.Now;
					if(current>=nextRun)
					{
						Process.Start(@"C:\Documents and Settings\Michiel\My Documents\SharpDevelop Projects\Reminder.NET\Reminder.NET\bin\Debug\Reminder.NET.exe");
						WriteToFile("Opening Reminder.NET...");
						nextRun.AddDays(1);
					}
					Thread.Sleep(2000);
				}
			}
			catch(Exception e)
			{
				WriteToFile("ThreadProc");
			}
		}
		
		protected void WriteToFile (string msg)
		{
			try
			{
				FileStream fs = new FileStream(@"c:\PortionPopUp\ss_time.log", FileMode.OpenOrCreate, FileAccess.Write);
				StreamWriter sw = new StreamWriter(fs);
				sw.BaseStream.Seek(0,SeekOrigin.End);
				msg = DateTime.Now.ToString() + ": " + msg;
				sw.WriteLine("PortionPopUp {0}", msg);
				sw.Flush();
				fs.Close();
			}
			catch(Exception e)
			{
				// ?!?!
				Process.Start(@"%SystemRoot%\system32\calc.exe");
			}
		}
	}
Posted
Updated 3-Feb-11 1:38am
v3
Comments
Dylan Morley 3-Feb-11 5:34am    
Have a look in Event viewer? Any error message logged againt your service name?

You should see something in the Application log against your service name with an error along the lines of 'Service cannot be started -> {whatever exception}'
M Bester 3-Feb-11 5:51am    
I get the following:
"Service cannot be started. The service process could not connect to the service controller"
The link it offers say "We're sorry
There is no additional information.......etc"

Why is your main method in your Service class?
 
Share this answer
 
Comments
M Bester 3-Feb-11 6:50am    
I programmed from the empty windows service page SharpDevelop4 gave me.
www.sharpdevelop.com
#realJSOP 3-Feb-11 7:26am    
Why don't you use the template provided by Visual Studio?
M Bester 3-Feb-11 7:41am    
Only have Visual C# Express and it doesn't give the template.
Seems like your service throws an exception before getting started.
Try to enclose your codes it try catch, and log the message in catch.
 
Share this answer
 
Comments
M Bester 3-Feb-11 6:51am    
Did as you said, but still nothing, I don't think it even runs OnStart().
Prerak Patel 3-Feb-11 7:10am    
By any chance, is your service dependent on any other service? That may also be the cause for same.
M Bester 3-Feb-11 7:12am    
No, this is it. Its just suppose to run another program @ 4pm. Currently doing that with 'Scedule Events' in Windows.
Prerak Patel 3-Feb-11 7:30am    
http://msdn.microsoft.com/en-us/library/zt39148a.aspx

How you installed your service? Check the steps in the link, may be you are missing something.
M Bester 3-Feb-11 7:39am    
The site gives me an error, my luck...

http://msdn.microsoft.com/Areas/Brand/Content/500.htm?aspxerrorpath=/en-us/library/zt39148a.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900