Files
XCEngine/docs/api/threading/task-group/cancel.md
ssdfasd 82cf147817 docs: 修正 API 文档准确性 (第四轮检查)
修复问题:
- containers: HashMap 实现描述修正
- debug: XE_LOG 宏参数顺序修正
- memory: ProxyAllocator 统计示例修正, PoolAllocator allocate size 检查描述
- resources: ResourceManager 缺失 UnloadGroup 方法
- rhi: D3D12 格式枚举名称修正, Texture Format 枚举补全, ResourceStates 补充
- threading: TaskGroup GetProgress/Wait/Cancel 实现限制说明
2026-03-19 00:43:16 +08:00

47 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# TaskGroup::Cancel
```cpp
void Cancel()
```
取消任务组中所有尚未执行的任务。正在执行的任务将不受影响。
**参数:**
**返回:**
**复杂度:** O(1)
**注意:**
- 已完成的任务不受影响。
- 正在执行的任务继续执行直到完成。
- 调用后所有 Wait/WaitFor 会立即返回(但请参见下方警告)。
- **警告**`Cancel()` 调用 `OnCancel()` 后不会减少待完成任务数,因此如果任务组中还有未执行的任务,调用 `Wait()` 会导致永久阻塞。建议在 `Cancel()` 后使用 `IsComplete()` 轮询或使用带超时的 `WaitFor()`
**示例:**
```cpp
TaskGroup* group = TaskSystem::Get().CreateTaskGroup();
for (int i = 0; i < 100; ++i) {
group->AddTask([i]() {
if (ShouldCancel()) {
return; // 检查取消状态
}
ProcessLongTask(i);
});
}
// 如果用户点击取消按钮
if (userClickedCancel) {
group->Cancel();
printf("Tasks canceled. Progress: %.1f%%\n", group->GetProgress() * 100.0f);
}
group->Wait();
```
## 相关文档
- [TaskGroup 总览](task-group.md) - 返回类总览