UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FAsyncTaskBase Class Referenceabstract

#include <AsyncWork.h>

+ Inheritance diagram for FAsyncTaskBase:

Public Member Functions

virtual ~FAsyncTaskBase ()
 
int64 GetRequiredMemory () const final
 
const TCHARGetDebugName () const final
 
void StartSynchronousTask (EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal, EQueuedWorkFlags InQueuedWorkFlags=EQueuedWorkFlags::None, int64 InRequiredMemory=-1, const TCHAR *InDebugName=nullptr)
 
void StartBackgroundTask (FQueuedThreadPool *InQueuedPool=GThreadPool, EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal, EQueuedWorkFlags InQueuedWorkFlags=EQueuedWorkFlags::None, int64 InRequiredMemory=-1, const TCHAR *InDebugName=nullptr)
 
void EnsureCompletion (bool bDoWorkOnThisThreadIfNotStarted=true, bool bIsLatencySensitive=false)
 
bool Reschedule (FQueuedThreadPool *InQueuedPool=GThreadPool, EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal)
 
bool Cancel ()
 
bool WaitCompletionWithTimeout (float TimeLimitSeconds)
 
bool IsDone ()
 
bool IsWorkDone () const
 
bool IsIdle () const
 
bool SetPriority (EQueuedWorkPriority QueuedWorkPriority)
 
EQueuedWorkPriority GetPriority () const
 

Protected Member Functions

void Init (TStatId InStatId)
 
void CheckIdle () const
 
virtual void DoTaskWork ()=0
 
virtual bool TryAbandonTask ()=0
 
virtual void MarkAsCanceled ()
 

Detailed Description

FAsyncTask - template task for jobs queued to thread pools

Sample code:

class ExampleAsyncTask : public FNonAbandonableTask
{
    friend class FAsyncTask<ExampleAsyncTask>;

    int32 ExampleData;

    ExampleAsyncTask(int32 InExampleData)
     : ExampleData(InExampleData)
    {
    }

    void DoWork()
    {
        ... do the work here
    }

    FORCEINLINE TStatId GetStatId() const
    {
        RETURN_QUICK_DECLARE_CYCLE_STAT(ExampleAsyncTask, STATGROUP_ThreadPoolAsyncTasks);
    }
};

void Example()
{

start an example job

    FAsyncTask<ExampleAsyncTask>* MyTask = new FAsyncTask<ExampleAsyncTask>( 5 );
    MyTask->StartBackgroundTask();

–or –

    MyTask->StartSynchronousTask();

to just do it now on this thread Check if the task is done :

    if (MyTask->IsDone())
    {
    }

Spinning on IsDone is not acceptable( see EnsureCompletion ), but it is ok to check once a frame. Ensure the task is done, doing the task on the current thread if it has not been started, waiting until completion in all cases.

    MyTask->EnsureCompletion();
    delete Task;
}

Constructor & Destructor Documentation

◆ ~FAsyncTaskBase()

virtual FAsyncTaskBase::~FAsyncTaskBase ( )
inlinevirtual

Destructor, not legal when a task is in process

Member Function Documentation

◆ Cancel()

bool FAsyncTaskBase::Cancel ( )
inline

Cancel the task, if possible. Note that this is different than abandoning (which is called by the thread pool at shutdown).

Returns
true if the task was canceled and is safe to delete. If it wasn't canceled, it may be done, but that isn't checked here.

◆ CheckIdle()

void FAsyncTaskBase::CheckIdle ( ) const
inlineprotected

Internal call to assert that we are idle

◆ DoTaskWork()

◆ EnsureCompletion()

void FAsyncTaskBase::EnsureCompletion ( bool  bDoWorkOnThisThreadIfNotStarted = true,
bool  bIsLatencySensitive = false 
)
inline

Wait until the job is complete

Parameters
bDoWorkOnThisThreadIfNotStartedif true and the work has not been started, retract the async task and do it now on this thread
specifiesif waiting for the task should return as soon as possible even if this delays other tasks

◆ GetDebugName()

const TCHAR * FAsyncTaskBase::GetDebugName ( ) const
inlinefinalvirtual

Returns text to identify the Work, for debug/log purposes only

Reimplemented from IQueuedWork.

◆ GetPriority()

EQueuedWorkPriority FAsyncTaskBase::GetPriority ( ) const
inline

◆ GetRequiredMemory()

int64 FAsyncTaskBase::GetRequiredMemory ( ) const
inlinefinalvirtual

Returns an approximation of the peak memory (in bytes) this task could require during it's execution.

Reimplemented from IQueuedWork.

◆ Init()

void FAsyncTaskBase::Init ( TStatId  InStatId)
inlineprotected

◆ IsDone()

bool FAsyncTaskBase::IsDone ( )
inline

Returns true if the work and TASK has completed, false while it's still in progress. prior to returning true, it synchronizes so the task can be destroyed or reused

◆ IsIdle()

bool FAsyncTaskBase::IsIdle ( ) const
inline

Returns true if the work has not been started or has been completed. NOT to be used for synchronization, but great for check()'s

◆ IsWorkDone()

bool FAsyncTaskBase::IsWorkDone ( ) const
inline

Returns true if the work has completed, false while it's still in progress. This does not block and if true, you can use the results. But you can't destroy or reuse the task without IsDone() being true or EnsureCompletion()

◆ MarkAsCanceled()

virtual void FAsyncTaskBase::MarkAsCanceled ( )
inlineprotectedvirtual

Mark the task as canceled (i.e. no longer needed).

◆ Reschedule()

bool FAsyncTaskBase::Reschedule ( FQueuedThreadPool InQueuedPool = GThreadPool,
EQueuedWorkPriority  InQueuedWorkPriority = EQueuedWorkPriority::Normal 
)
inline

If not already being processed, will be rescheduled on given thread pool and priority.

Returns
true if the reschedule was successful, false if was already being processed.

◆ SetPriority()

bool FAsyncTaskBase::SetPriority ( EQueuedWorkPriority  QueuedWorkPriority)
inline

◆ StartBackgroundTask()

void FAsyncTaskBase::StartBackgroundTask ( FQueuedThreadPool InQueuedPool = GThreadPool,
EQueuedWorkPriority  InQueuedWorkPriority = EQueuedWorkPriority::Normal,
EQueuedWorkFlags  InQueuedWorkFlags = EQueuedWorkFlags::None,
int64  InRequiredMemory = -1,
const TCHAR InDebugName = nullptr 
)
inline

Queue this task for processing by the background thread pool

◆ StartSynchronousTask()

void FAsyncTaskBase::StartSynchronousTask ( EQueuedWorkPriority  InQueuedWorkPriority = EQueuedWorkPriority::Normal,
EQueuedWorkFlags  InQueuedWorkFlags = EQueuedWorkFlags::None,
int64  InRequiredMemory = -1,
const TCHAR InDebugName = nullptr 
)
inline

Run this task on this thread

Parameters
bDoNowif true then do the job now instead of at EnsureCompletion

◆ TryAbandonTask()

◆ WaitCompletionWithTimeout()

bool FAsyncTaskBase::WaitCompletionWithTimeout ( float  TimeLimitSeconds)
inline

Wait until the job is complete, up to a time limit

Parameters
TimeLimitSecondsMust be positive, otherwise polls – same as calling IsDone()
Returns
true if the task is completed

The documentation for this class was generated from the following file: