{"record":{"id":"876e011166bf5d0a","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-876e01","errorCode":null,"errorMessage":"重启服务 {serv_name} 时失败。未找到该服务！","messagePattern":"重启服务 (.+?) 时失败。未找到该服务！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":263,"sourceCode":"                        //如果在轮询期间，没有返回true，那么最终返回false\n                        return false;\n                    }\n                }\n                catch (Exception Ex)\n                {\n                    new Log(Ex.ToString());\n                    return false;\n                }\n            }\n\n            internal static bool Restart(string serv_name)\n            {\n                try\n                {\n                    //未创建服务，不能停止！\n                    if (!Query.IsCreated(serv_name))\n                    {\n                        throw new Exception($\"重启服务 {serv_name} 时失败。未找到该服务！\");\n                    }\n\n                    //已安装服务，开始重启\n                    if (Stop(serv_name))\n                    {\n                        if (Start(serv_name))\n                        {\n                            return true;        //当且仅当停止成功，开启成功，返回true。除此之外，均为false\n                        }\n                    }\n\n                    return false;\n                }\n                catch (Exception Ex)\n                {\n                    new Log(Ex.ToString());\n                    return false;\n                }","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L245-L281","documentation":"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.","triggerScenarios":"Calling Action.Restart for an unregistered or misspelled service, or when ServiceController.GetServices() fails so IsCreated returns false from its catch.","commonSituations":"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.","solutions":["Confirm the service is registered (Query.IsCreated) before restarting.","Verify the ServiceName spelling with `sc query`.","On false return, distinguish 'absent' from 'stop/start timeout' by re-checking IsCreated.","If timeouts are the real issue, widen the poll or stop/start manually first."],"exampleFix":"// before\nbool ok = Com_ServiceOS.Action.Restart(servName);\n\n// after\nif (!Com_ServiceOS.Query.IsCreated(servName)) { return false; }\nbool ok = Com_ServiceOS.Action.Restart(servName);","handlingStrategy":"validation","validationCode":"if (!Com_ServiceOS.Query.IsCreated(servName)) { return false; }\nbool ok = Com_ServiceOS.Action.Restart(servName);","typeGuard":"static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);","tryCatchPattern":null,"preventionTips":["Pre-check IsCreated before Restart; Restart returns false if absent or if Stop/Start times out.","Confirm ServiceName spelling with `sc query`.","If Stop or Start times out (>10s each), widen the operation or act manually first.","Run elevated; both Stop and Start require privilege."],"tags":["windows-service","scm","service-restart"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}