Files
XCSDD/docs/api/threading/task-group/cancel.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

46 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(n)
**当前状态:** 此方法会调用各任务的 `OnCancel()` 回调,但不会减少 `m_pendingCount` 计数器。因此调用 `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);
}
// 注意Cancel() 后 Wait() 会永久阻塞,应使用 WaitFor()
group->WaitFor(std::chrono::seconds(1));
```
## 相关文档
- [TaskGroup 总览](task-group.md) - 返回类总览