C# Windows服务桌面问题
我有一个Windows服务,我从许多博客和论坛中收集了它,主要是我在这里问过和回答过的问题。服务很好。唯一的问题是当我停止服务时;当我停止它时,我在日志文件中看到的是进一步向下粘贴的内容C# Windows服务桌面问题,c#,.net,service,windows-services,C#,.net,Service,Windows Services,我有一个Windows服务,我从许多博客和论坛中收集了它,主要是我在这里问过和回答过的问题。服务很好。唯一的问题是当我停止服务时;当我停止它时,我在日志文件中看到的是进一步向下粘贴的内容 public partial class GBBInvService : ServiceBase { private static readonly ILog log = LogManager.GetLogger(typeof(GBBInvService)); System.Timers.Tim
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:01062[6]信息GBBInvService.GBBInventoryService[(空)]–内部停止
2013-04-23 14:53:01062[6]信息GBBInvService.GBBInventoryService[(null)]–未请求停止
它进入了(!\u requestStop)
的内部,而不是的外部
。我的代码错了吗?有人能给我解释一下为什么它在(!\u requestStop)
中而不是在else语句中
如果有任何建议,我将不胜感激,因为我刚刚开始接触Windows服务和最近的日志记录。我看不出有什么问题。您的逻辑永远不会改变_requestStop=true。这总是假的
!!如果为true,则false肯定会通过阻塞。除非某些内容发生更改,否则它将始终为false ServiceBase没有自动将_requestStop设置为true的代码,您的程序在任何地方都不会更改它 您的代码正在按预期运行 当Windows服务管理器发出停止请求时,将运行OnStop()。更多信息请访问 你会
_requestStop = true
在OnStop()的顶部,向程序的其余部分发送信号以完成任何任务
也就是说,我不知道你想要这个程序做什么。我可以提供更多关于它应该做什么的详细建议。根据您共享的代码,我认为有一些事情正在发生:
- 正如其他人所指出的,如果没有
\u requestStop=true代码>某处,
将始终计算为_requestStop
。然后,不可能执行true
中的OnStop()
。随着else
添加到volatile
的声明中,您可能希望操作系统修改它,但是声明应该是\u requestStop
;即使这样,操作系统也不会自动修改公共的
。因此,是的,关于\u requestStop
并期望执行\u requestStop
,您的代码似乎是错误的else
- 您使用
似乎表明您不信任\u requestStop
只会在应该调用它的时候被调用(即当请求停止时)。作为Windows服务的新手,我想我可以看到这一点,但这种不信任是没有道理的OnStop()
- 正如您所指出的,您对日志记录还不熟悉,您在IMO上的日志记录太多了;有时少就是多
\u 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();
}
}