0

Can I make a Task wait for an IEnumerator to end? I have a Task that in one point starts an IEnumerator, but i want the task to wait until the IEnumerator end. (its an IEnumerator that plays an animation , something like "while(x != y) {Do something}")


private async Task TaskNAME()
{
   code
   code
   code
   StartCoroutine(IEnimeratorName(VALUE));
}

IEnumerator IEnimeratorName()
 {
   while (valuee != targerValue)
   {
    move recttransform
    yield return null;
   }
 }

Id like to await for the IEnimeratorName to finish and only then return the TaskNAME

  • You could await any `Async` task within any `Async` task, please post some code to get more help – Ibram Reda Nov 03 '20 at 04:12
  • You can use `TaskCompletionSource`. Create one, get the `Task` property for wherever you want to `await`, and then set the result after you're done enumerating. See duplicate for the basic idea (it's about C# events, but the principle is identical) – Peter Duniho Nov 03 '20 at 04:13
  • Base on the code you posted, create the `TaskCompletionSource` object in `TaskName()`, pass it to `IEnimeratorName()`, have the latter method call `SetResult()` on it _after_ the `while` loop completes, and have the former `await` the `Task` property of the `TaskCompletionSource` object. You don't actually care about the result in this case, so you can use whatever type you like for `T` (`bool` is a reasonable choice). – Peter Duniho Nov 03 '20 at 04:22
  • By the way, the word is spelled "enumerator", not "enimerator". Programming is not a practice where carelessness is a reasonable habit. – Peter Duniho Nov 03 '20 at 04:22

0 Answers0