【问题标题】:execute command crashing on start pending status c#执行命令在启动挂起状态时崩溃c#
【发布时间】:2013-11-08 15:48:28
【问题描述】:

我正在用 c# 编写一个 button_click 函数,在其中我使用 executeCommand 与服务对话。

ServiceController sc = new ServiceController("MyService");
int command = 145;
sc.Stop();
//writing xml file
sc.Start();
sc.ExecuteCommand(command);

有时程序崩溃,无法调用执行命令,因为服务处于启动/停止挂起状态。我该怎么办? 我需要停止/启动服务,因为该函数必须编写相同的 xml 文件。

【问题讨论】:

    标签: c# servicecontroller


    【解决方案1】:

    添加try-catch-finally块,看看是否有异常导致服务崩溃。

    try{
         ServiceController sc = new ServiceController("MyService");
         int command = 145;
         sc.Stop();
         //writing xml file
         sc.Start();
    }
    catch(Exception ex)
    {
         throw ex;
    }
    finally{
         sc.ExecuteCommand(command);
    }
    

    【讨论】:

    • 是 System.InvalidOperationException,无法启动服务“MyService”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多