Windowservice error

柔情痞子 提交于 2019-12-04 14:54:00

问题


I have solution which contain so many projects and windowservices. I modified coding on the application side of windowservice,after that i copied the exe regarding that service(F:\Components\Console\Bin\service.exe) in to the installation path(C:\Program Files\syscon\ Monitor\service.exe) after stopping windowservice from the 'services.msc'.Now i am getting the value on service while debugging which is not getting previously.But now when i start the service from 'services.msc' i am getting this error "Could not start the operational sentinel windows resource monitor service on local computer.Error 1053: The service did not respond to the start or control request in a timely fashion" Can any tell me a solution for this


回答1:


We create many services during our work and one method for debugging I've found very useful is the following:

Make your service a "dual application" that can run as a service or as a normal Windows Forms application, controlled by some command line parameter. I pass "/gui" to my services.

In void Main(string[] args) I check for the parameter:

If it is missing, I execute the code to instantiate the service (the code generated by Visual Studio).

If it is there, I run code to create a normal Windows Forms application, where the main form instantiates the service and calls OnStart and OnStop accordingly (you'll have to create wrapper methods due to visibility of OnStart and OnStop).

Of course you have to add the references to the Windows Forms assemblies manually and add code like Application.Run(...) yourself, but debugging capabilities are greatly improved without going through the hassle of the "Stop Service, Copy File, Start Service, Fail"-routine.

Most likely, there's a bug in your OnStart code which makes the instance drop out of that routine and the service manager keeps waiting.

EDIT
Here's a code sample for you of the way I create the service/gui depending on the parameter:

static void Main(string[] args)
{
    if (args.Length > 0 && args[0].Equals("/gui"))
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmGui());
    }
    else
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new SampleService() };
        ServiceBase.Run(ServicesToRun);
    }
}


来源:https://stackoverflow.com/questions/1788263/windowservice-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!