I’ve spent the day working through Async.h implementing task management for the various design patterns I use in my C# apps. It’s been a C++ slog but I finally have compiling & funcitonal boilerplate for each use case except for one.
Specifically, How do i go about ensuring the completion callback contains the result of the Future’s Get method without resorting to a class level variable for the result of the Async call?
The below is what I’m thinking, but naturally result_E.Get() isn’t valid in it’s current location
Hi there, you’re not far off in your approach however you need to correctly capture the result after the async task has finished. The main issue in your code is that you’re trying to capture the result of result_E.Get() before the future has been resolved, which isn’t valid.
Something along these lines:
void FWortWortWort::DoWork() {
// Define the task to be executed asynchronously.
TFunction<FString()> task_E = [this]() { return this->Async_TestE(); };
// Create a promise to set the result and a future to get the result.
TPromise<FString> promise_E;
TFuture<FString> result_E = promise_E.GetFuture();
// Define the completion callback to handle the result.
TUniqueFunction<void()> callback = [this, result_E = MoveTemp(result_E)]() mutable {
FString strYarp = result_E.Get();
OnTask_E_Completed(strYarp);
};
// Run the task asynchronously and fulfill the promise when done.
Async(EAsyncExecution::ThreadPool, [promise_E = MoveTemp(promise_E), task_E = MoveTemp(task_E), callback = MoveTemp(callback)]() mutable {
FString result = task_E();
promise_E.SetValue(result); // Set the result.
callback(); // Invoke the callback with the result.
});
UE_LOG(LogVTSFileManager, Log, TEXT("Some Other Work"));
}
FString FWortWortWort::Async_TestE() {
UE_LOG(LogVTSFileManager, Log, TEXT("Start Async E"));
FPlatformProcess::Sleep(2); // Simulate long computation.
UE_LOG(LogVTSFileManager, Log, TEXT("End Async E"));
return TEXT("Return Async E");
}
void FWortWortWort::OnTask_E_Completed(FString strValue) {
UE_LOG(LogVTSFileManager, Log, TEXT("Async E Completed: %s"), *strValue);
}