Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

(solved) Task.Delay on Coroutine

Discussion in 'Experimental Scripting Previews' started by eunpyoung, Feb 13, 2018.

  1. eunpyoung

    eunpyoung

    Joined:
    Jan 23, 2018
    Posts:
    2
    UnityVersion : 2017.3.1f1

    when I call
    StartCoroutine(CoPP());
    Unity just Freezed.

    Is it bug? or not?
    how to solve that problem?


    public IEnumerator CoPP()
    {
    Debug.Log(1);
    var task = HelloAsync();
    Debug.Log(2);
    yield return task;
    Debug.Log(3);

    Debug.Log(task.Result);
    }

    async Task<int> HelloAsync()
    {
    await Task.Delay(TimeSpan.FromSeconds(1));

    return 9999;
    }
     
  2. eunpyoung

    eunpyoung

    Joined:
    Jan 23, 2018
    Posts:
    2
    sorry that's my mistake.that cause by my misundestand of task.


    public static class TaskExtensions
    {
    public static IEnumerator AsIEnumerator(this Task task)
    {
    while (!task.IsCompleted)
    {
    yield return null;
    }

    if (task.IsFaulted)
    {
    throw task.Exception;
    }
    }
    }

    i can solve using this.

    yield return task.AsIEnumerator();