OdysseusYuan/LKY_OfficeTools · error · Exception

重启服务 {serv_name} 时失败。未找到该服务!

Error message

重启服务 {serv_name} 时失败。未找到该服务!

What it means

Thrown inside Com_ServiceOS.Action.Restart when Query.IsCreated(serv_name) returns false (Com_ServiceOS.cs:261-263). Restart composes Stop then Start; the missing-service guard runs before that composition. The throw is caught by Restart's own catch and Restart returns false (Com_ServiceOS.cs:277-281), so Restart only returns true when both Stop and Start succeed.

Source

Thrown at LKY_OfficeTools/Common/Com_ServiceOS.cs:263

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

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

                    //已安装服务,开始重启
                    if (Stop(serv_name))
                    {
                        if (Start(serv_name))
                        {
                            return true;        //当且仅当停止成功,开启成功,返回true。除此之外,均为false
                        }
                    }

                    return false;
                }
                catch (Exception Ex)
                {
                    new Log(Ex.ToString());
                    return false;
                }

View on GitHub (pinned to 6f9a1bd471)

Solutions

  1. Confirm the service is registered (Query.IsCreated) before restarting.
  2. Verify the ServiceName spelling with `sc query`.
  3. On false return, distinguish 'absent' from 'stop/start timeout' by re-checking IsCreated.
  4. If timeouts are the real issue, widen the poll or stop/start manually first.

Example fix

// before
bool ok = Com_ServiceOS.Action.Restart(servName);

// after
if (!Com_ServiceOS.Query.IsCreated(servName)) { return false; }
bool ok = Com_ServiceOS.Action.Restart(servName);
Defensive patterns

Strategy: validation

Validate before calling

if (!Com_ServiceOS.Query.IsCreated(servName)) { return false; }
bool ok = Com_ServiceOS.Action.Restart(servName);

Type guard

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

Prevention

When it happens

Trigger: Calling Action.Restart for an unregistered or misspelled service, or when ServiceController.GetServices() fails so IsCreated returns false from its catch.

Common situations: Attempting to restart a service that an earlier uninstall step already removed; targeting the wrong machine state in a deployment script; name copied from the services.msc display column instead of the ServiceName.

Related errors


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