窗口服务OnStop没有被调用 [英] Window service OnStop not getting called

查看:122
本文介绍了窗口服务OnStop没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows服务.当我尝试停止时,它将进入停止状态,而当我将断点放置在onStop方法中时,它就不会受到打击.无法调试出什么问题.我已经在整个服务中添加了异常处理,还添加了未处理的异常处理,但是没有收到任何错误.只要将canStop设置为true并使用RequestAdditionalTime(),服务便可以正常工作.

I have a C# Windows service. 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. 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().

我还在app.config中设置了legacyUnhandledExceptionPolicy enabled ="true". 对我来说,这看起来像是一些线程/资源问题,阻止了服务控制管理器调用OnStop方法.否则服务可能已处于损坏状态,并且服务控制管理器无法将其停止.只是将状态从停止"更改为正在停止".

I have also set legacyUnhandledExceptionPolicy enabled="true" in app.config. For me it looks like some threading/resource issue that prevent service control manager to call the OnStop method. 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.

当我尝试通过将代码附加到服务exe进行调试时,尝试停止服务时没有任何反应.不会调用onstop方法.

When I try debugging by attaching code to service exe nothing happens when try to stop the service. The onstop method is not getting called.

在Onstart方法中,启动一个线程,该线程每30秒进行一次轮询.假设调用了onStop方法,则它将中止该线程.这就是开发此服务的方式.

In Onstart method starts a thread that keep doing a kind of polling on every 30 second. Suppose onStop method gets called then it will abort this thread. This is how this service is developed.

如果存在某些线程问题,或者我的OnStart方法中的线程阻塞了服务控制管理器可用的资源,是否有一些免费的工具可用于调试?

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?

OnStop方法中的代码:

Code in OnStop method:

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继续运行,直到服务运行.它需要进行轮询. 如果调用onStop方法(不被调用)将中止此OnStartThread.

OnStartThread goes on running until service is running. It required to do a polling. If onStop method is call (which is not being called) will abort this OnStartThread.

推荐答案

主要问题是查找问题所在.因此继续注释和取消注释代码的不同部分,发现了真正的问题.OnStartthread启动了一个进程(启动一个exe),因此我只将此代码保留在Windows服务中并删除了所有内容,因此能够重现问题.so我所做的是在启动进程后让OnStartthread睡眠1秒钟. (更合适的方法是启动该过程并捕获不同的事件以了解该过程的状态,并在准备就绪时就可以移动它)就像魔术一样起作用. 这以某种方式解决了我面临的问题.

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. ( 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.

当没有睡眠时,进程将启动,但是我无法在OnStop方法上达到断点,因为进程(exe)已启动,但是它需要更多资源才能完全启动并保持一致并放置线程.sleep将所需的资源提供给exe才能完全启动.

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.

    //the code is in OnStartThread 

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

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

这篇关于窗口服务OnStop没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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