Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Define an Async Action in c#?

I really want to know if there is a way to define action variables async? Or some alternative method?

public System.Action myAction;

public async System.Action myAsyncAction;
void Start()
{
    // normal action
    myAction += () =>
    {
        Debug.Log("Inject some code in runtime..");
    };

    // I want something like this that support wait time..
    myAsyncAction += () =>
    {
        await Task.Delay(2000f);
        
        Debug.Log("Inject some code in runtime..");
    };
}
like image 333
KiynL Avatar asked Feb 02 '26 21:02

KiynL


1 Answers

I've used Func<Task> in the past. EG:

Func<Task> asyncMethod = async () =>
{
    await Task.Delay(1000);
    Console.WriteLine("done here");
};

await asyncMethod();
like image 63
Clinton Avatar answered Feb 04 '26 11:02

Clinton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!