[英]Windows service OnStop issue
我有一个Windows服务,该服务是由许多博客和论坛汇集而成的,主要是我在这里提出和回答的问题。 该服务工作正常。 唯一的问题是我何时停止服务? 停止粘贴时,我在日志文件中看到的是粘贴的内容。
public partial class GBBInvService : ServiceBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(GBBInvService));
System.Timers.Timer timer = new System.Timers.Timer();
private volatile bool _requestStop=false;
private ManualResetEventSlim resetEvent = new ManualResetEventSlim(false);
public GBBInvService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
_requestStop = false;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 18000;
timer.Enabled = true;
timer.Start();
log.Info("GBBInvService Service Started");
}
protected override void OnStop()
{
log.Info("inside stop");
if (!_requestStop)
{
log.Info("Stop not requested");
timer.Start();
}
else
{
log.Info("On Stop Called");
WaitUntilProcessCompleted();
}
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
log.Info("Timer elapsed at " + Convert.ToString(e.SignalTime));
InvProcessing();
}
private void InvProcessing()
{
try
{
resetEvent.Reset();
//*Processing here*
}
catch (Exception ex)
{
resetEvent.Set();
log.Error(ex.Message);
}
}
private void WaitUntilProcessCompleted()
{
resetEvent.Wait();
}
}
该服务正常停止并再次正常启动,但是我不知道我的代码是否错误,因为日志文件正在显示:
2013-04-23 14:53:01,062 [6]信息GBBInvService.GBBInventoryService [(null)] –内部停止
2013-04-23 14:53:01,062 [6] INFO GBBInvService.GBBInventoryService [(null)] –不要求停止
它在(!_requestStop)
而不是else
。 我的代码错了吗? 有人可以向我解释为什么它在(!_requestStop)
而不是else语句内。
任何建议将不胜感激,因为我才刚刚开始动手使用Windows服务和最近的日志记录。
除非_requestStop发生更改,否则它将始终为false。
ServiceBase没有代码可以自动将_requestStop设置为true,并且您的程序不会在任何地方对其进行更改。
您的代码正在按预期运行。
从Windows服务管理器请求停止时,将运行OnStop()。 请参阅http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicebase.onstop.aspx
你会设置
_requestStop = true
在OnStop()的顶部以向程序的其余部分发出信号以完成所有任务。
就是说,我不知道您希望该程序做什么。 我可以提供更多有关它应该做什么的更多详细信息。
根据您共享的代码,我认为发生了一些事情:
_requestStop = true;
在某处, !_requestStop
始终将评估为true
。 这样就不可能执行OnStop()
中的else
。 随着volatile
添加到_requestStop
的声明,也许你期待的操作系统进行修改,但随后的声明应该public
; 即使这样,操作系统也不会自动修改_requestStop
。 因此,是的,对于_requestStop
并期望else
能够执行,您的代码似乎是错误的。 _requestStop
使用似乎表明,您不信任OnStop()
仅在应调用OnStop()
时才被调用(即,当请求停止时)。 我想我是Windows服务的新手,但是这种不信任是没有必要的。 除非您尚未共享的代码使用_requestStop
,否则我将针对具体问题和您对代码提出的问题提出以下建议:
public partial class GBBInvService : ServiceBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(GBBInvService));
System.Timers.Timer timer = new System.Timers.Timer();
//private volatile bool _requestStop=false; // no _requestStop
private ManualResetEventSlim resetEvent = new ManualResetEventSlim(false);
public GBBInvService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//_requestStop = false;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 18000;
timer.Enabled = true;
timer.Start();
log.Info("GBBInvService Service Started");
}
protected override void OnStop()
{
//log.Info("inside stop");
//if (!_requestStop)
//{
// log.Info("Stop not requested");
// timer.Start();
//}
//else
//{
// log.Info("On Stop Called");
// WaitUntilProcessCompleted();
//}
WaitUntilProcessingCompleted();
log.Info("GBBInvService Service Stopped");
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
log.Info("Timer elapsed at " + Convert.ToString(e.SignalTime));
InvProcessing();
}
private void InvProcessing()
{
try
{
resetEvent.Reset();
//*Processing here*
}
catch (Exception ex)
{
log.Error(ex.Message);
}
finally
{
resetEvent.Set();
}
}
private void WaitUntilProcessCompleted()
{
resetEvent.Wait();
}
}
我看不出有什么问题。 您的逻辑永远不会改变_requestStop = true。 总是错误的。
!false肯定会经过if-true块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.