【发布时间】:2011-09-25 07:24:29
【问题描述】:
我有一个我不知道运行时间的服务,我猜大概是 7 秒。由于某种原因,该服务在第一次运行后停止工作,我无法调试它。它一直在服务管理器上说“开始”,我在附加进程窗口中找不到它。
当我尝试停止它时,停止按钮只出现一秒钟。即使我按下它,我也会收到一条错误消息,提示“Windows 无法停止服务 'Splive 在本地计算机上。服务没有返回错误。这可能是内部 Windows 错误或内部服务错误。”
处理此问题的最佳方法是什么?
static void Main(string[] args)
{
ServiceBase.Run(new Program());
ServiceController service = new ServiceController();
service.ServiceName = "SpLive";
service.Start();
//Sp objSportingbet = new Sp();
//objSportingbet.getListingsFromSp();
}
public Program()
{
this.ServiceName = "SpLive";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
objSportingbet.getListingsFromSp();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 7000;
timer1.Enabled = true;
timer1.Start();
}
protected override void OnStop()
{
base.OnStop();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 7000;
timer1.Enabled = false;
timer1.Start();
}
private void timer1_Elapsed(object sender, EventArgs e)
{
ServiceController service = new ServiceController();
service.ServiceName = "Sp";
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
}
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
}
timer1.Stop();
}
private void InitializeComponent()
{
//
// Program
//
this.CanPauseAndContinue = true;
this.CanShutdown = true;
}
【问题讨论】:
-
使用异常处理并记录异常...它将帮助您找到问题
-
视情况而定,但您检查过您的事件日志吗?
-
我应该在 OnStart() 和 OnStop() 中添加一个例外 我没有从 GetListingsFromSp() 获得例外
-
这段代码是在您的服务中还是在某些测试程序中?因为
service.Stop()应该停止您的服务,如果这是您服务中的代码,这对我来说似乎很奇怪(看这里:msdn.microsoft.com/en-us/library/…)
标签: c# windows windows-services