UniTask icon indicating copy to clipboard operation
UniTask copied to clipboard

How to mimic TaskStatus.Running from C# Tasks in UniTasks?

Open EricBatlleTripledot opened this issue 1 year ago • 5 comments

Hey, wonderful package, you deserve a monument for this.

I'm just wondering, UniTask have a lot of UniTaskStatus options, but I'm missing the equivalent to TaskStatus.Running, why? How can I try to know if a Task is Running? The only way I can think of is to do:

public static bool IsRunning(this UniTask uniTask)
{
    return uniTask.Status is not (UniTaskStatus.Canceled and UniTaskStatus.Faulted and UniTaskStatus.Pending and UniTaskStatus.Succeeded);
}

But this, by default it's imposible, cause there's no any other state :/

Could you suggest a better way? I need this to also work with Defered tasks, and this issue makes me think that maybe accessing and comparing Status is not the best way :(

EricBatlleTripledot avatar Dec 12 '24 16:12 EricBatlleTripledot

Since UniTask may always be returned to the pool, checking with IsRunning and then performing some execution may not be safe. Because this safety cannot be guaranteed, we cannot officially provide checking points. This is a trade-off that comes with the allocation-free design.

neuecc avatar Dec 13 '24 01:12 neuecc

Thanks for the quick answer. Understood, it is a limitation of the framework. Are you aware about any workaround, knowing the current limitation of the system?

Could be a good idea to reflect that on the Readme? (I can do it myself if you prefer)

EricBatlleTripledot avatar Dec 13 '24 15:12 EricBatlleTripledot

I oppose including Status in the ReadMe since it's primarily an internal flag that should only be used when absolutely necessary by people who understand the internal workings.

neuecc avatar Dec 16 '24 01:12 neuecc

@EricBatlleTripledot: just a minor hint, but if you write extension methods for structs, it's typically advantageous to pass them around by reference, either using ref or in.

RichardWepner avatar Jun 08 '25 12:06 RichardWepner