Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ServiceBase之后控制台应用程序代码中的C#windows服务。是否运行未执行?_C#_Windows Services - Fatal编程技术网

ServiceBase之后控制台应用程序代码中的C#windows服务。是否运行未执行?

ServiceBase之后控制台应用程序代码中的C#windows服务。是否运行未执行?,c#,windows-services,C#,Windows Services,我在控制台应用程序中实现了windows服务,如下所示: internal class Program { private static void Main(string[] args) { ServiceBase.Run(new MyServicesInitializer()); //host the services in managed windows service //some more code st

我在控制台应用程序中实现了windows服务,如下所示:

  internal class Program
{
    private static void Main(string[] args)
    {

            ServiceBase.Run(new MyServicesInitializer()); //host the services in managed windows service

         //some more code
         string x=1;
         .....

    }



 public class MyServicesInitializer : ServiceBase
 {
       protected override void OnStart(string[] args)
       {
               //my code
       }
 }
我的问题是:当我用sc.exe启动服务时,是否调用了main方法?好像不是。。。如果有人能解释我使用sc启动服务时发生的情况以及我需要该行的原因:ServiceBase.Run(new MyServicesInitializer())在我的主菜单中


编辑:我在main中的行之前和之后进行了实验并抛出异常:当我在抛出异常之前抛出异常时,但是当我在run方法之后抛出异常时,它没有被抛出,并且服务成功启动。。。有人可以解释为什么Run方法之后的代码没有执行?

我设法弄清楚发生了什么,下面是流程:
当函数ServiceBase.Run(newmyservicesinitializer())运行时直到服务停止后,代码才会从该函数返回,因此只有在服务停止后,代码才会运行

我也不会指望这一点,因为这是无证行为。更安全的假设是,在main函数中唯一可以做的事情就是调用
ServiceBase.Run
。但是我实现了两种模式:用于开发的控制台模式和用于生产的windows服务模式。。。但重要的是要知道,当服务在执行代码后停止代码时。。。在我的例子中,在函数ServiceBase.Run之后添加了return语句。