Windows服务将无法启动 [英] Windows service won't stay started

查看:81
本文介绍了Windows服务将无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 在这里的伙计们的帮助下,编写了一个小型Windows服务,但它不会永远启动.它开始然后停止,而不创建它假定的日志文件.尚未完成,但继续这样下去毫无意义.
我在某处错过了什么吗?

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?

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

推荐答案

为什么您的主要方法在Service类中?
Why is your main method in your Service class?


似乎您的服务引发了异常在开始之前.
尝试将尝试捕获的代码封装起来,然后将消息记录在catch中.
Seems like your service throws an exception before getting started.
Try to enclose your codes it try catch, and log the message in catch.


这篇关于Windows服务将无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆