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
- 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.
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
- 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.
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
- 启动服务 {serv_name} 时失败。未找到该服务!
- 重启服务 {serv_name} 时失败。未找到该服务!
- 创建服务 {serv_name} 时,返回值为空!
- 服务 {serv_name} 因无法停止,导致卸载失败!
- 删除服务 {serv_name} 时,返回值为空!
AI-assisted analysis of OdysseusYuan/LKY_OfficeTools@6f9a1bd471 (2026-08-13).
Data as JSON: /api/errors/e51eeeb1dba01592.
Report an issue: GitHub.