相关疑难解决方法(0)

C#Windows服务

脚本

我已经创建了一个Windows服务,但每当我启动它时,它立即停止.该服务来自一个控制台应用程序,该应用程序用于订阅事件并监视服务器上的进程.如果进程发生任何事情(即它被杀死),那么该事件将触发重新启动进程.我告诉你的原因是因为原来的代码看起来像这样:

原始控制台应用代码:

    static void Main(string[] args)
    {
        StartProcess sp = new StartProcess();
        //Note the readline that means the application sits here waiting for an event!
        Console.ReadLine(); 
    }
Run Code Online (Sandbox Code Playgroud)

现在这段代码已经变成了Windows服务,它基本上就是完全相同的.然而,服务并没有坐在那里等待,即使有readline,它只是结束.....

新的Windows服务代码:

    protected override void OnStart(string[] args)
    {
        ProcessMonitor pm = new ProcessMonitor();
        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

思考

由于功能完全封装在这个单独的类中(它完全开始,设置一些事件和等待) - 我怎样才能让服务真正坐在那里等待?它似乎忽略了readline.然而,这完全可以作为控制台应用程序使用,将它作为服务更方便.

c# windows-services

0
推荐指数
1
解决办法
1378
查看次数

标签 统计

c# ×1

windows-services ×1