void Task4(IProgressPromise <float, string> promise) { Debug.Log("The task4 start"); int n = 10; StringBuilder buf = new StringBuilder(); for (int i = 1; i <= n; i++) { /* If the task is cancelled, then stop the task */ if (promise.IsCancellationRequested) { promise.SetCancelled(); break; } buf.Append(i).Append(" "); promise.UpdateProgress(i / (float)n); #if NETFX_CORE Task.Delay(100).Wait(); #else Thread.Sleep(100); #endif } promise.SetResult(buf.ToString()); /* set a result of the task */ Debug.Log("The task4 end"); }
/// <summary> /// Simulate a task. /// </summary> /// <returns>The task.</returns> /// <param name="promise">Promise.</param> protected IEnumerator DoTask(IProgressPromise <Progress, string> promise) { int n = 50; Progress progress = new Progress(); progress.TotalBytes = n; progress.bytes = 0; StringBuilder buf = new StringBuilder(); for (int i = 0; i < n; i++) { /* If the task is cancelled, then stop the task */ if (promise.IsCancellationRequested) { promise.SetCancelled(); yield break; } progress.bytes += 1; buf.Append(" ").Append(i); promise.UpdateProgress(progress); /* update the progress of task. */ yield return(new WaitForSeconds(0.01f)); } promise.SetResult(buf.ToString()); /* update the result. */ }
/// <summary> /// Simulate a task. /// </summary> /// <returns>The task.</returns> /// <param name="promise">Promise.</param> protected void DoTask(IProgressPromise <float, string> promise) { try { int n = 50; float progress = 0f; StringBuilder buf = new StringBuilder(); for (int i = 0; i < n; i++) { /* If the task is cancelled, then stop the task */ if (promise.IsCancellationRequested) { promise.SetCancelled(); break; } progress = i / (float)n; buf.Append(" ").Append(i); promise.UpdateProgress(progress);/* update the progress of task. */ #if NETFX_CORE Task.Delay(200).Wait(); #else Thread.Sleep(200); #endif } promise.UpdateProgress(1f); promise.SetResult(buf.ToString()); /* update the result. */ } catch (System.Exception e) { promise.SetException(e); } }
IEnumerator Task4(IProgressPromise <float, string> promise) { Debug.Log("The task4 start"); int n = 10; StringBuilder buf = new StringBuilder(); for (int i = 1; i <= n; i++) { /* If the task is cancelled, then stop the task */ if (promise.IsCancellationRequested) { promise.SetCancelled(); yield break; } buf.Append(i).Append(" "); promise.UpdateProgress(i / (float)n); yield return(null); } promise.SetResult(buf.ToString()); /* set a result of the task */ Debug.Log("The task4 end"); }