✅ #async #Unity how to do something after tasks were complete and not block main threads with tasks
i have tasks that can take very log time to complete in unity
and at the moment i launch tasks this way
async void StartCuttingObjects()
{
foreach (var g in objectsToCut)
{
//i have removed some irrelevant code and if you wanna remove parameters in response it is fine
var t = Task.Run(() => Cutter.AsyncCut(cutPlane,
originalPos,
originalRot,
originalScale,
mf,
mr,
mesh
, subMeshCount,
triangles,
vertices,
normals,
uv,
mats,
g
));
tasks.Add(t);
}
FooAsync();
}
public async void FooAsync()
{
await Task.WhenAll(tasks);
EndCuttingObjects();
}
59 Replies
learn how to properly use async / await and Tasks
a function that is async should always return Task unless its an event handler, only then async void is allowed
and any async method should get
await
ed
if you call without await
then its not running asynchronously, its running synchronously and blocks your program
async
keyword doesnt make anything run asynchronously, await
does
async only allows you to use await
in a method
@yaroj3456Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
TLDR on
async
/await
:
* every .net API that is suffixed with Async
(eg: .Read()
and .ReadAsync()
) => use the Async
version
* if the API name ends with Async
=> await
it
* if the API returns a Task
=> await
it
* if you have to await
in a method:
* that method must have the async
keyword (you could even suffix your function name with Async
, up to you)
* if it was returning T
(eg:string
) => it should now return Task<T>
(eg: Task<string>
)
* APIs to ban and associated fix:
do you have control over the command? could add that event handlers use
async void
only*changing
public async void FooAsync()
to public async Task FooAsync()
didnt fix the issuewell there is much more that I've said and the bot said
- make same change on
StartCuttingObjects
- make sure any async method gets awaited, that includes Task.Run
, FooAsync
, StartCuttingObjects
, all of these must get awaitedi awaited that FooAsync and it didnt help if you are about that
this
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
@TeBeClone he send this before reading my last message, give him some time to think about it
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
what implication?
awaiting StartCuttingObjects?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
ah that
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I'm following, you got a point
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
StartCuttingObjects returning void?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
we can assume its not a handler tho
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I don't see what the void version provides to us other than the ability to block the program
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
my point is this is not a handler. I'm not aware of other cases where async void is fine, are there even any?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
yeah
did you mean await Parallel.ForEach(
cause visual studio doesnt recognise foreachasync
.net version?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
what .net is unity using?
I dont know game dev
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
even if its not actual async work the goal was to not block the main thread by scheduling the work on another thread
so question should be answerd now by TeBeClone
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
yeah
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
wait
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
no it is my custom made task
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
LoL okay I just thought for a sec they dont use TPL
yikes
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
yeah cpu bound
no io
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I agree
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
make even AsyncCut synchronous and just wrap in Task.Run if its just cpu bound
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
but should be not at least await Task.Run @TeBeClone ? you know better then me, I'm just asking
no critique
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
fair
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
just that StartCuttingObjects is now blocking ^^ it shouldnt be tho or why would we use Task.Run() in the first place. Or do I have a brain fart here
@TeBeClone
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I mean the ultimate goal was to write non blocking code and this looks very blocking to me
,well initially there was a task.run
but on smartphone there is always a lag when I start cutting
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
From my understanding this would be best for @yaroj3456
The Cut method is synchronous cuz its nothing that would require asynchronous working. But we wrap it in something non blocking to not block main thread?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View