Hello,
I’m trying to create UI where there is lot of processing happens in background and UI gets updated and I wanted to use multi threading to do the processing. So I came up with this method to dynamically create and update UI from different threads.
Create scene will create a small widget and put it into a vertical panel in the UI this is all done in blueprint.
I know UE4 crashes if I try to create a UObject from any thread other than game thread so every time I want to create a UObject I can create a ASync from thread on game thread and create UObject inside the ASync.
Header file:
class COOPGAMETOOLS_API UCoOpToolsWidget : public UUserWidget
{
GENERATED_BODY()
public:
void AsyncCreateScene();
protected:
UFUNCTION(BlueprintImplementableEvent, Category = "CoOpToolsWidget")
USceneWidget* CreateScene();
};
class FCreateSceneWorker : public FRunnable
{
bool ShouldThreadRun;
/** Thread to run the worker FRunnable on */
FRunnableThread* Thread;
public:
FCreateSceneWorker(UCoOpToolsWidget* pCoOpToolsWidget);
~FCreateSceneWorker();
// Begin FRunnable interface.
virtual bool Init();
virtual uint32 Run();
virtual void Stop();
UCoOpToolsWidget* CoOpToolsWidget;
};
Code:
void UCoOpToolsWidget::AsyncCreateScene()
{
AsyncTask(ENamedThreads::GameThread, [&]() {
USceneWidget* SceneWidget = CreateScene();
});
}
FCreateSceneWorker::FCreateSceneWorker(UCoOpToolsWidget* pCoOpToolsWidget) :
CoOpToolsWidget(pCoOpToolsWidget)
{
Thread = FRunnableThread::Create(this, TEXT("FCreateSceneWorker"), 0, TPri_BelowNormal);
}
FCreateSceneWorker::~FCreateSceneWorker()
{
delete Thread;
}
bool FCreateSceneWorker::Init()
{
ShouldThreadRun = true;
return true;
}
uint32 FCreateSceneWorker::Run()
{
//Initial wait before starting
UE_LOG(LogTemp, Warning, TEXT("Worker Start"));
FPlatformProcess::Sleep(0.03);
for (int i = 0; i < 100 && ShouldThreadRun; ++i)
{
FPlatformProcess::Sleep(1.0);
CoOpToolsWidget->AsyncCreateScene();
}
UE_LOG(LogTemp, Warning, TEXT("Worker Done"));
return 0;
}
void FCreateSceneWorker::Stop()
{
ShouldThreadRun = false;
}
And I create the thread
FCreateSceneWorker* Runnable = new FCreateSceneWorker(this);
It works, but i want to make sure that there wont be any problems in future.
I do get a warning
LogStats: Warning: MetaData mismatch. Did you assign a stat to two groups? New //STATGROUP_Threads//FCreateSceneWorker///Thread_1ab4_0///####STATCAT_Advanced#### old //STATGROUP_Threads//FCreateSceneWorker///Thread_23dc_0///####STATCAT_Advanced####
I want to know if what i’m doing is correct and if not how can I correct my approach?
What is this warning and how can i remove the warning?
Thanks