Concept Flow - Task handle usage
Create Task
Store Task Handle
Use Handle to Control Task
Delete or Suspend Task Using Handle
End
Create a task and save its handle to control it later, like suspending or deleting.
TaskHandle_t xHandle = NULL;
void vTaskCode(void * pvParameters) {
for(;;) {
// Task work here
}
}
xTaskCreate(vTaskCode, "Task1", 1000, NULL, 1, &xHandle);| Step | Action | Variable/Handle State | Result/Output |
|---|---|---|---|
| 1 | Declare xHandle as NULL | xHandle = NULL | No task created yet |
| 2 | Define task function vTaskCode | No change | Task code ready |
| 3 | Call xTaskCreate with &xHandle | xHandle assigned task handle | Task created and handle stored |
| 4 | Task runs in background | xHandle valid | Task executes loop |
| 5 | Use xHandle to suspend task | xHandle unchanged | Task suspended |
| 6 | Use xHandle to delete task | xHandle unchanged | Task deleted |
| 7 | End of usage | xHandle still holds handle value | Handle can be reused or set NULL |
| Variable | Start | After xTaskCreate | After Suspend | After Delete |
|---|---|---|---|---|
| xHandle | NULL | Valid Task Handle | Valid Task Handle | Valid Task Handle (handle value remains) |
Task Handle Usage in FreeRTOS: - Declare TaskHandle_t variable (e.g., xHandle) - Pass &xHandle to xTaskCreate to get handle - Use handle to suspend, resume, or delete task - Handle remains valid until task is deleted - Handle is NULL before task creation