Files
XCEngine/docs/api/threading/task/task.md
ssdfasd 6a952473ce docs: fix threading module documentation discrepancies
- Fix include paths: use #include "Threading/..." instead of <XCEngine/Threading/...>
- Document protected ITask constructors (ITask(), ITask(TaskPriority))
- Document Callback typedef in TaskGroup
- Clarify Mutex STL-compatible methods are const
- Note GetProgress() implementation limitation (returns 0.0f)
2026-03-19 00:49:08 +08:00

100 lines
2.5 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.
# ITask
**命名空间**: `XCEngine::Threading`
**类型**: `class` (abstract)
**头文件**: `XCEngine/Threading/Task.h`
**描述**: 任务基类抽象接口和任务状态/优先级枚举定义。
## 概述
`ITask` 是任务系统的核心抽象基类。用户需要继承此类并实现 `Execute()` 方法来定义具体的任务逻辑。任务系统通过引用计数自动管理任务生命周期。
## TaskPriority 枚举
任务优先级枚举(数值越小优先级越高)。
| 值 | 描述 |
|----|------|
| `Critical` | 关键优先级0 |
| `High` | 高优先级1 |
| `Normal` | 普通优先级2 |
| `Low` | 低优先级3 |
| `Idle` | 空闲优先级4 |
## TaskStatus 枚举
任务状态枚举。
| 值 | 描述 |
|----|------|
| `Pending` | 等待中 |
| `Scheduled` | 已调度 |
| `Running` | 运行中 |
| `Completed` | 已完成 |
| `Failed` | 失败 |
| `Canceled` | 已取消 |
## ITask 公共方法
### 生命周期
| 方法 | 描述 |
|------|------|
| `ITask()` | 默认构造函数(受保护) |
| `ITask(TaskPriority priority)` | 带优先级的构造函数(受保护) |
| [`Execute`](execute.md) | 任务执行逻辑(纯虚) |
| [`OnComplete`](oncomplete.md) | 任务完成回调(可重写) |
| [`OnCancel`](oncancel.md) | 任务取消回调(可重写) |
### 属性
| 方法 | 描述 |
|------|------|
| [`GetPriority`](getpriority.md) | 获取优先级 |
| [`GetStatus`](getstatus.md) | 获取状态 |
| [`GetId`](getid.md) | 获取任务 ID |
| [`SetId`](setid.md) | 设置任务 ID |
| [`SetPriority`](setpriority.md) | 设置优先级 |
| [`SetStatus`](setstatus.md) | 设置状态 |
### 引用计数
| 方法 | 描述 |
|------|------|
| [`AddRef`](addref.md) | 增加引用计数 |
| [`Release`](release.md) | 减少引用计数(引用归零时自动 delete |
## 使用示例
```cpp
class MyTask : public ITask {
public:
explicit MyTask(int data) : m_data(data) {}
void Execute() override {
printf("Task executed with data: %d\n", m_data);
}
void OnComplete() override {
printf("Task completed\n");
}
private:
int m_data;
};
auto task = new MyTask(42);
task->SetPriority(TaskPriority::High);
TaskSystem::Get().Submit(std::unique_ptr<ITask>(task));
```
## 相关文档
- [LambdaTask](../lambdatask/lambdatask.md) - Lambda 任务封装
- [TaskGroup](../task-group/task-group.md) - 任务组
- [TaskSystem](../task-system/task-system.md) - 任务系统
- [../threading/threading.md](../threading.md) - 模块总览