OdysseusYuan/LKY_OfficeTools · error · Exception

停止服务 {serv_name} 时失败。未找到该服务!

Error message

停止服务 {serv_name} 时失败。未找到该服务!

What it means

Thrown inside Com_ServiceOS.Action.Stop when Query.IsCreated(serv_name) returns false (Com_ServiceOS.cs:212-214). Identical mechanism to Action.Start: the throw is logged and Stop returns false (Com_ServiceOS.cs:249-253), so callers see a bool, not an exception. Stop also sends control.Stop() only when Status != Stopped, then polls up to 10s for the Stopped state.

Source

Thrown at LKY_OfficeTools/Common/Com_ServiceOS.cs:214

                        //如果在轮询期间,没有返回true,那么最终返回false
                        return false;
                    }
                }
                catch (Exception Ex)
                {
                    new Log(Ex.ToString());
                    return false;
                }
            }

            internal static bool Stop(string serv_name)
            {
                try
                {
                    //未创建服务,不能停止!
                    if (!Query.IsCreated(serv_name))
                    {
                        throw new Exception($"停止服务 {serv_name} 时失败。未找到该服务!");
                    }

                    //已安装服务,开始停止
                    using (var control = new ServiceController(serv_name))
                    {
                        //只要不是停止状态,就发送停止指令
                        if (control.Status != ServiceControllerStatus.Stopped)
                        {
                            control.Stop();
                        }

                        //判断状态,是否是停止了
                        int wait_time = 0;                          //等待时间总计
                        while (wait_time <= (10 * 1000))            //最多等待10秒
                        {
                            //获取服务状态
                            if (Query.RunState(serv_name) == (int)ServiceControllerStatus.Stopped)
                            {

View on GitHub (pinned to 6f9a1bd471)

Solutions

  1. Pre-check Query.IsCreated(serv_name) and skip Stop when absent (idempotent uninstall).
  2. Verify the exact ServiceName via `sc query` before invoking Stop.
  3. Treat the false return as 'not stopped in time or not present' and branch accordingly.
  4. If the service exists but will not stop within the 10s poll, stop it manually with `sc stop` / `net stop` first.

Example fix

// before
Com_ServiceOS.Action.Stop(servName);

// after
if (Com_ServiceOS.Query.IsCreated(servName))
{
    Com_ServiceOS.Action.Stop(servName);
}
Defensive patterns

Strategy: validation

Validate before calling

if (Com_ServiceOS.Query.IsCreated(servName))
    Com_ServiceOS.Action.Stop(servName);
// else: nothing to stop

Type guard

static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);

Prevention

When it happens

Trigger: Calling Action.Stop for a service that is not registered, was already removed, or whose name does not match any entry returned by ServiceController.GetServices(). Also when GetServices throws so IsCreated falls through to its catch and yields false.

Common situations: Cleanup/uninstall path running after the service was already deleted; name drift after a rename; running on a minimal Server Core image where the service was never present.

Related errors


AI-assisted analysis of OdysseusYuan/LKY_OfficeTools@6f9a1bd471 (2026-08-13). Data as JSON: /api/errors/e51eeeb1dba01592. Report an issue: GitHub.