简体   繁体   English

窗口服务OnStop没有被调用

[英]Window service OnStop not getting called

I have a C# Windows service. 我有C#Windows服务。 When I try to stop it goes in stopping state and when I placed the break point in onStop method then it is not getting hit. 当我尝试停止时,它将进入停止状态,而当我将断点放置在onStop方法中时,它就不会受到打击。 Not able to debug what is problem. 无法调试什么问题。 I have added exception handling all over the service and also unhandled exception handling but I don't get any error. 我在整个服务中都添加了异常处理,还添加了未处理的异常处理,但是没有收到任何错误。 Every thing is fine with the servie it has canStop set to true and also uses RequestAdditionalTime(). 可以将canStop设置为true并使用RequestAdditionalTime()的服务正常运行。

I have also set legacyUnhandledExceptionPolicy enabled="true" in app.config. 我还在app.config中设置了legacyUnhandledExceptionPolicy enabled =“ true”。 For me it looks like some threading/resource issue that prevent service control manager to call the OnStop method. 对我来说,这看起来像是一些线程/资源问题,阻止了服务控制管理器调用OnStop方法。 Or the service may be in already corrupt state and service control manager can not stop it. 或者服务可能已处于损坏状态,并且服务控制管理器无法停止它。 It just change the state from Stop to Stopping. 它只是将状态从“停止”更改为“正在停止”。

When I try debugging by attaching code to service exe nothing happens when try to stop the service. 当我尝试通过将代码附加到服务exe进行调试时,尝试停止服务时没有任何反应。 The onstop method is not getting called. 不会调用onstop方法。

In Onstart method starts a thread that keep doing a kind of polling on every 30 second. 在Onstart方法中,启动一个线程,该线程在每30秒进行一次轮询。 Suppose onStop method gets called then it will abort this thread. 假设调用了onStop方法,那么它将中止该线程。 This is how this service is developed. 这就是开发此服务的方式。

Is there some free tools available to debug if there is some threading issue or my thread in OnStart method is chocking the resource available to service control manager? 如果存在某些线程问题,或者我的OnStart方法中的线程阻塞了服务控制管理器可用的资源,是否有一些免费的工具可用于调试?

Code in OnStop method: OnStop方法中的代码:

try
{
    RequestAdditionalTime(10*60*1000);
    IntPtr handle = this.ServiceHandle;
    KServiceStatus.currentState = (int) State.SERVICE_START_PENDING;
    SetServiceStatus(handle, ref KServiceStatus);

    RequestAdditionalTime(30000);

    if ((onStartThread == null) ||
         ((onStartThread.ThreadState &
          (ThreadState.Unstarted | ThreadState.Stopped)) != 0))
    {
        onStartThread = new Thread(new ThreadStart(KWindowsServiceHandler.OnStart));
        onStartThread.Start();
    }

    KServiceStatus.currentState = (int) State.SERVICE_RUNNING;
    SetServiceStatus(handle, ref KServiceStatus);
 }              

OnStartThread goes on running until service is running. OnStartThread继续运行直到服务运行。 It required to do a polling. 它需要进行轮询。 If onStop method is call (which is not being called) will abort this OnStartThread. 如果onStop方法被调用(未调用),则会中止此OnStartThread。

Try to use Debugger.Break(); 尝试使用Debugger.Break(); at the point, where you want to break execution. 此时,您想中断执行。 Or attach to windows service process via menu Debug > Attach to process and set breakpoint in Visual Studio. 或通过菜单Debug > Attach to process和在Visual Studio中设置断点Debug > Attach to process附加到Windows服务进程。

UPDATE: If debugger is not attached to process, you can do that this way 更新:如果调试器未附加到进程,则可以通过这种方式进行

if (!Debugger.IsAttached)
    Debugger.Launch();

Why not to use System.Threading.Timer to do polling each 30 seconds? 为什么不使用System.Threading.Timer每30秒进行一次轮询?

protected override void OnStart(string[] args)
{
    const int period = 30 * 1000;
    Timer timer = new Timer(TimerCallback, null, 0, period);
}

private void TimerCallback(object state)
{
    // Polling goes here
}

Main problem was to find what the problem is. 主要问题是找到问题所在。 So went on commenting and uncommenting different part of code and found the real issue.OnStartthread launches a process ( which starts one exe) So i kept just this code in the my windows service and removed everything and i was able to reproduce the issue.so what I did is let the OnStartthread sleep for 1s after starting the process. 因此继续注释和取消注释代码的不同部分,发现了真正的问题.OnStartthread启动了一个进程(启动一个exe),因此我只将此代码保留在Windows服务中并删除了所有内容,因此能够重现问题.so我所做的是在启动进程后让OnStartthread睡眠1秒钟。 ( the more appropriate would be to start the process and catch the different events to know the status of the process and when it is ready just move) it worked like magic. (更合适的方法是启动该过程并捕获不同的事件以了解该过程的状态,并在准备就绪时就可以移动它)就像魔术一样起作用。 This is somehow fixes the issue that i was facing. 这以某种方式解决了我面临的问题。

When this sleep was not there then process would start but I was not able to hit the break point on OnStop method because the process (exe) got started but it needed some more resource to start fully and be consistent and putting the thread.sleep give the required resource to exe to start fully. 当没有睡眠时,进程将启动,但由于进程(exe)已启动,但我无法达到OnStop方法的断点,但它需要更多资源才能完全启动并保持一致并放入线程。 exe完全启动所需的资源。

    //the code is in OnStartThread 

someProcess.start(); someProcess.start(); Thread.Sleep(1000); Thread.Sleep(1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM