{"record":{"id":"e51eeeb1dba01592","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-e51eee","errorCode":null,"errorMessage":"停止服务 {serv_name} 时失败。未找到该服务！","messagePattern":"停止服务 (.+?) 时失败。未找到该服务！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":214,"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 Stop(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                    using (var control = new ServiceController(serv_name))\n                    {\n                        //只要不是停止状态，就发送停止指令\n                        if (control.Status != ServiceControllerStatus.Stopped)\n                        {\n                            control.Stop();\n                        }\n\n                        //判断状态，是否是停止了\n                        int wait_time = 0;                          //等待时间总计\n                        while (wait_time <= (10 * 1000))            //最多等待10秒\n                        {\n                            //获取服务状态\n                            if (Query.RunState(serv_name) == (int)ServiceControllerStatus.Stopped)\n                            {","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L196-L232","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pre-check Query.IsCreated(serv_name) and skip Stop when absent (idempotent uninstall).","Verify the exact ServiceName via `sc query` before invoking Stop.","Treat the false return as 'not stopped in time or not present' and branch accordingly.","If the service exists but will not stop within the 10s poll, stop it manually with `sc stop` / `net stop` first."],"exampleFix":"// before\nCom_ServiceOS.Action.Stop(servName);\n\n// after\nif (Com_ServiceOS.Query.IsCreated(servName))\n{\n    Com_ServiceOS.Action.Stop(servName);\n}","handlingStrategy":"validation","validationCode":"if (Com_ServiceOS.Query.IsCreated(servName))\n    Com_ServiceOS.Action.Stop(servName);\n// else: nothing to stop","typeGuard":"static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);","tryCatchPattern":null,"preventionTips":["Make Stop idempotent: only call it when IsCreated is true.","Distinguish 'absent' from 'timeout' by re-checking IsCreated after a false return.","For stubborn services, stop manually with `sc stop` first and wait for Stopped.","Run elevated so control.Stop() has the required privilege."],"tags":["windows-service","scm","service-stop"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}