// Copyright UnexGames 2025. All Rights Reserved. #include "ThreadTasks.h" bool UThreadTasks::Start() { if (IsRunning()) { return false; } bCanceled = false; EAsyncExecution AsyncType = EAsyncExecution::Thread; switch (ExecutionType) { case ETaskExecutionType::TaskGraph: AsyncType = EAsyncExecution::TaskGraph; break; case ETaskExecutionType::Thread: AsyncType = EAsyncExecution::Thread; break; case ETaskExecutionType::ThreadPool: AsyncType = EAsyncExecution::ThreadPool; break; } UThreadTasks* TaskWorker = this; TFunction BodyFunc = [TaskWorker]() { if (TaskWorker != nullptr && TaskWorker->IsValidLowLevel() && IsValid(TaskWorker) && !TaskWorker->IsUnreachable()) { TaskWorker->TaskBody(); if (TaskWorker->TaskDelegate.IsBound()) { TaskWorker->TaskDelegate.Broadcast(); } } }; TFunction OnCompleteFunc = [TaskWorker]() { AsyncTask(ENamedThreads::GameThread, [TaskWorker]() { if (TaskWorker != nullptr && TaskWorker->IsValidLowLevel() && IsValid(TaskWorker) && !TaskWorker->IsUnreachable()) { if (!TaskWorker->IsCanceled()) { TaskWorker->OnComplete(); } else { TaskWorker->OnCancel(); } } }); }; Tasks.SetNumZeroed(1); if (AsyncType == EAsyncExecution::ThreadPool && ThreadPool && ThreadPool->GetThreadsNum() > 0) { Tasks[0] = AsyncPool(ThreadPool->Obj.ToSharedRef().Get(), TUniqueFunction(BodyFunc), TUniqueFunction(OnCompleteFunc)); // Async functions, it create an new threadpool and execute functions on it. } else { Tasks[0] = Async(AsyncType, TUniqueFunction(BodyFunc), TUniqueFunction(OnCompleteFunc)); // Async functions, create new thread and execute functions on it. } return true; } void UThreadTasks::TaskBody_Implementation() { }