{"record":{"id":"63013589b56059ae","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-630135","errorCode":null,"errorMessage":"服务 {serv_name} 因无法停止，导致卸载失败！","messagePattern":"服务 (.+?) 因无法停止，导致卸载失败！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":342,"sourceCode":"                    }\n                }\n                catch (Exception Ex)\n                {\n                    new Log(Ex.ToString());\n                    return false;\n                }\n            }\n\n            internal static bool Delete(string serv_name)\n            {\n                if (Query.IsCreated(serv_name))\n                {\n                    //已经创建服务，才能删除\n\n                    //先停止服务\n                    if (!Action.Stop(serv_name))\n                    {\n                        throw new Exception($\"服务 {serv_name} 因无法停止，导致卸载失败！\");\n                    }\n\n                    string cmd_del = $\"sc delete \\\"{serv_name}\\\"\";\n                    var del_result = Com_ExeOS.Run.Cmd(cmd_del);\n\n                    //非空判断，若返回值为空，则为假\n                    if (string.IsNullOrEmpty(del_result))\n                    {\n                        throw new Exception($\"删除服务 {serv_name} 时，返回值为空！\");\n                    }\n\n                    //判断是否删除成功\n                    if (del_result.Contains(\"成功\") || del_result.ToLower().Contains(\"success\"))\n                    {\n                        return true;\n                    }\n                    else\n                    {","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L324-L360","documentation":"Thrown in Com_ServiceOS.Config.Delete when Action.Stop(serv_name) returns false before issuing `sc delete` (Com_ServiceOS.cs:340-343). Unlike Create/Modify, Delete has NO try/catch, so this exception PROPAGATES to the caller. Stop returns false when the service is absent, when control.Stop() throws (e.g. not enough privilege), or when the service does not reach Stopped within the 10s poll window.","triggerScenarios":"Calling Config.Delete on a service that will not stop within 10 seconds (long shutdown), depends on other running services, requires elevation to stop, or whose process is hung and ignores the stop control.","commonSituations":"Uninstall routine while the service's host process is hung; stopping a service blocked by dependent services; non-elevated host process; service in StopPending forever.","solutions":["Wrap the Config.Delete call in try/catch since Delete does not swallow its own exceptions.","Stop the service manually first with `sc stop` / `net stop` and wait for Stopped before calling Delete.","Run the host process elevated so control.Stop() has the required privilege.","Kill a hung service host (taskkill /F) if graceful stop never completes, then retry Delete."],"exampleFix":"// before\nCom_ServiceOS.Config.Delete(servName);\n\n// after\ntry { Com_ServiceOS.Config.Delete(servName); }\ncatch (Exception ex) when (ex.Message.Contains(\"无法停止\"))\n{\n    // force-stop then retry once\n}","handlingStrategy":"try-catch","validationCode":"// Pre-stop to avoid the failure path\nif (Com_ServiceOS.Query.IsCreated(servName) && !Com_ServiceOS.Action.Stop(servName))\n{ /* warn: graceful stop failed; consider force */ }","typeGuard":null,"tryCatchPattern":"try\n{\n    Com_ServiceOS.Config.Delete(servName);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"无法停止\"))\n{\n    // Stop returned false before delete; force-stop host or widen timeout, then retry.\n}","preventionTips":["Config.Delete has NO internal try/catch; always wrap it in try/catch.","Stop the service manually first and wait for Stopped before calling Delete.","Run elevated so control.Stop() succeeds.","Kill a hung service host with taskkill /F if it never stops, then retry Delete."],"tags":["windows-service","scm","service-uninstall","unhandled-exception"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}