AsyncTask Function

AsyncTask提供了一种简洁的方法,在不创建额外工作类或宏的情况下,在单独的线程中执行代码并传递参数。虽然不能取消AsyncTask,但一旦运行,它会一直运行到操作完成。通常用于处理一些耗时的操作,如在C++中计算素数。可以通过静态蓝图函数实现,并通过委托每找到第1000个素数时触发回调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a simple way of executing code in a seperate thread as well as pass in parameters without any workers or runnable tasks.
AsyncTask’s cannot be canceled, once it runs, it runs until the action is complete!

What is it?
If you require a quick way of just handling some demanding code on a thread without creating a whole worker class or some weird extra macros then the AsyncTask Function is what your looking for! Lets look how we can implement it.

Example Video

The basic structure looks pretty much like this:

// Schedule a task on any thread with a high or normal priority
AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [Params ...] ()
{
        
     // Do some stuff here... 
        
	// Once the task is done schedule a task back to the gamethread.
    // This is optional depending on the situation, you don't have to go back to the mainthread. 
	AsyncTask(ENamedThreads::GameThread, [Params ...] ()
	{
              // Return something or execute a delegate 
	});
});

AsyncTask can also be used for pretty much anything in C++, just make sure that incase you want to edit say an actors values asynchronously that you pass in a TWeakObjectPtr of that object in order to do so.

Implementing AsyncTask in a static BP Function
Lets implement it in a classic scenario of counting prime numbers, we’re going to use a BlueprintFunctionLibrary and implement a static function that then counts the prime numbers and executes a delegate every 1000th find.

//HEADER
#pragma once

// Core includes
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"

// generated.h
#include "BPFunctionTest.generated.h"

// Our delegate to return our value
DECLARE_DYNAMIC_DELEGATE_OneParam(FTestDelegate, FString, StringOut);

UCLASS()
class YOURPROJECT_API UBPFunctionTest : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
	/** Executes every 1000th Prime count */
	UFUNCTION(BlueprintCallable, Category = "AsyncTest", meta=(DisplayName="AsyncTest", Keywords = "async"))
        static void AsyncPrimeCount(FTestDelegate Out, int32 PrimeCount);
	
};

Most of the code here is not important, the only parts that are important are the AsyncTask functions, the delegate and when we execute each. In short its really straight forward.

//CPP
#include "BPFunctionTest.h"

void UBPFunctionTest::AsyncPrimeCount(FTestDelegate Out, int32 PrimeCount)
{
    // Schedule a thread                               // Pass in our parameters to the lambda expression
    AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [Out, PrimeCount] ()
    {
        // This whole code just loops through numbers and counts how many prime nums it finds
        int PrimesFound = 0;
        int TestNumber = 2;

        while (PrimesFound < PrimeCount)
        {
            bool bIsPrime = true;

            for (int i = 2; i < TestNumber / 2; i++)
            {
                if (TestNumber % i == 0)
                {
                    bIsPrime = false;
                    break;
                }
            }

            if (bIsPrime)
            {
                PrimesFound++;

                // Every 1000 prime...
                if(PrimesFound % 1000 == 0)
                {
                    // We schedule back to the main thread and pass in our params
                    AsyncTask(ENamedThreads::GameThread, [Out, PrimesFound] ()
                    {
                        // We execute the delegate along with the param
                        Out.ExecuteIfBound(FString::FromInt(PrimesFound)); 
                    });
                }
            }
            TestNumber++;
        }
    });
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值