question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way of executing a task synchronously?

See original GitHub issue

Thanks 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
neuecccommented, Aug 31, 2020

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.

0reactions
xuciancommented, Aug 31, 2020

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)?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found