- 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
46 lines
1.1 KiB
Markdown
46 lines
1.1 KiB
Markdown
# 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) - 返回类总览
|