简体   繁体   English

C#:Windows控制台应用程序到Windows服务

[英]C#: Windows Console Application to Windows Service

I have a console application that tracks the performance of the computer, and I am trying to make it into a Windows Service. 我有一个控制台应用程序,它可以跟踪计算机的性能,并且正在尝试使其成为Windows服务。 The service installs fine, however when I go into task manager and try to run the service I get Error 1053: the service did not respond to the start or control request in a timely fashion. 该服务安装得很好,但是当我进入任务管理器并尝试运行该服务时,出现错误1053:该服务未及时响应启动或控制请求。 In the task manager the service sometimes has a "starting" status, but it will not run. 在任务管理器中,服务有时会处于“正在启动”状态,但不会运行。 On top of that, when I run the console app, I also get an error stating that: Cannot start service from the command line or a debugger. 最重要的是,当我运行控制台应用程序时,我还收到一条错误消息,指出: 无法从命令行或调试器启动服务。 A windows service must first be installed - which I have done. 必须先安装Windows服务 -我已经完成了。 This is my program class: 这是我的程序类:

    public static string ServiceName = "performanceService";

    static void Main(string[] args)
    {

        if (!Environment.UserInteractive)
        {
            PerformanceCounter ramCount = new PerformanceCounter("Memory", "Available MBytes");
        PerformanceCounter cpuCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        Console.WriteLine("Press any key to view other information...\n");
        Console.WriteLine("CPU and RAM information");
        while (!Console.KeyAvailable)
        {
            double perf = cpuCount.NextValue();
            Console.WriteLine("CPU Performance: " + perf + " %");

... Code continues on that calculates the performance and displays it in the console... 代码继续计算性能并将其显示在控制台中。

 }
        else
        {                
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
            new ServiceControl()
            };
            ServiceBase.Run(ServicesToRun);
        }
}

This is my ServiceControl class: 这是我的ServiceControl类:

public ServiceControl()
    {
        ServiceName = Program.ServiceName;
        InitializeComponent();
    }

    public void Start()
    {
        OnStart(null);

    }
    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

    }
    protected override void OnStop()
    {
        base.OnStop();
    }

    private void InitializeComponent()
    {
        // 
        // ServiceControl
        // 
        this.ServiceName = "performanceService";
    }

To install the service I have used both the Project Installer and manual install, just out of curiosity to be honest. 为了安装该服务,老实说,出于好奇,我同时使用了Project Installer和手动安装。

I changed the time limit through regedit, and that did not help. 我通过regedit更改了时间限制,但这没有帮助。

Please let me know if you need to see additional code. 如果您需要查看其他代码,请告诉我。

My question is, am I missing something from the code, that will not allow my service to start? 我的问题是,我是否在代码中缺少某些内容,从而无法启动我的服务? Thanks for your help. 谢谢你的帮助。

Thank you for all your help. 谢谢你的帮助。 The problem was that I had the Start() method in the ServiceControl class and I had my code the other way around in the Program class. 问题是我在ServiceControl类中具有Start()方法,而在Program类中却具有其他代码。

I was supposed to have the code to start the service in the if statement and the performance logic in the else, so that the start method executes quickly, like one of the comments suggested. 我应该在if语句中具有启动服务的代码,在else中具有性能逻辑,以便启动方法可以快速执行,就像建议的注释之一一样。 After clicking start on the service in the task manager, the status changes to "running". 在任务管理器中单击“启动”后,状态变为“正在运行”。

Thank you again. 再次感谢你。

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

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