No way of executing a task synchronously?
See original GitHub issueThanks for this library. Just started using it and still trying to figure out if it’s a good fit for me
I need to migrate parts of my code to use async/await in Unity, but I need to take small steps, so some async tasks should be executed synchronously (should block the thread so nothing else gets executed at the same time and break something). I tried AsTask().Wait() and AsTask().RunSynchronously(), but the first freezes unity while the latter throws an error.
As a simple example (note that the actual ones are much more complex):
public async UniTask WriteToResourcesAsync(DBDBuildInfo info)
{
await UniTask.Run(() => _MNGInstance.WriteData(LOCAL_PATH_IN_PROJECT, info));
UnityEditor.AssetDatabase.ImportAsset(LOCAL_PATH_IN_PROJECT, UnityEditor.ImportAssetOptions.ForceUpdate);
}
I want to call this both with await and synchronously. How can I achieve that?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How would I run an async Task<T> method synchronously?
Here's a workaround I found that works for all cases (including suspended dispatchers). It's not my code and I'm still working to fully ......
Read more >Task.RunSynchronously Method (System.Threading.Tasks)
The task to be run synchronously must be in the Created state. A task may be started and run only once. Any attempts...
Read more >How to call an async method synchronously in C# - ...
In C#, you can call an asynchronous function from a synchronous function by using the Result property or the Wait method of the...
Read more >Using Task.Run in Conjunction with Async/Await
One way to turn a synchronous operation into an asynchronous one is to run it on a separate thread, and that's where Task.Run...
Read more >How to call asynchronous method from synchronous ...
To call async method from a non async method we can use Task.Run method. Task.Run is to execute CPU-bound code in an asynchronous...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Waiting for synchronization is not recommended, even for Task. Task -> UniTask conversion is not a special consideration, but UniTask doesn’t fully support waiting for synchronization, so If you want to wait for synchronization, complete it only in Task.
Oh, no, the IO is purely .NET. And I have other tasks that use non-Unity code as well. You say I don’t need UniTask, but what about the example I’ve provided where I need to return UniTask? I don’t see much info about the interop between Task and UniTask. Can you provide some bullet points (unless they aren’t already written somewhere and I missed them)?