private static Task <int> ReturnValueAsyncWithoutAssistance() { AsyncTaskMethodBuilder <int> builder = AsyncTaskMethodBuilder <int> .Create(); try { Task <int> task = Task <int> .Factory.StartNew(() => 5); TaskAwaiter <int> awaiter = task.GetAwaiter(); if (!awaiter.IsCompleted) { // Result wasn't available. Add a continuation, and return the builder. // We really want: awaiter.OnCompleted(goto afterAwait); awaiter.OnCompleted(() => { try { int tmp2 = awaiter.GetResult(); builder.SetResult(tmp2 + 1); } catch (Exception e) { builder.SetException(e); } }); return(builder.Task); } // Result was already available: proceed synchronously afterAwait: int tmp = awaiter.GetResult(); builder.SetResult(tmp + 1); } catch (Exception e) { builder.SetException(e); } return(builder.Task); }
public void MoveNext() { int result; try { #pragma warning disable 0219 // doFinallyBodies is never used bool doFinallyBodies = true; #pragma warning restore if (state != 1) { if (state != -1) { task = Task <int> .Factory.StartNew(() => 5); awaiter = task.GetAwaiter(); if (awaiter.IsCompleted) { goto Label_GetResult; } state = 1; doFinallyBodies = false; awaiter.OnCompleted(moveNextDelegate); } return; } state = 0; Label_GetResult: // target of state=1 int awaitResult = awaiter.GetResult(); awaiter = default(TaskAwaiter <int>); result = awaitResult; } catch (Exception e) { state = -1; builder.SetException(e); return; } state = -1; builder.SetResult(result); }
public void MoveNext() { int result; try { bool doFinallyBodies = true; int tmpState = state; // Effectively a three way switch: 1, -1, 0 if (tmpState != 1) { if (state == -1) { return; } this.writer = File.CreateText("output.txt"); } try { tmpState = state; if (tmpState == 1) { goto Label_ResumePoint; } sum = 0; i = 0; Label_ResumePoint: // This shouldn't quite be here... see below while (i < loopCount) { // Not in generated code: if (state == 1) { goto Label_ResumePoint2; } // Back to generated code valueFetcher = Task.Factory.StartNew(() => 1); j = 0; // Still not in the generated code, and still not quite right... we don't want the j test here Label_ResumePoint2: // Back to generated code again... while (j < 3) { try { // We want Label_ResumePoint to be here really tmpState = state; if (tmpState != 1) { Console.WriteLine("Awaiting..."); awaiter = valueFetcher.GetAwaiter(); if (!awaiter.IsCompleted) { state = 1; doFinallyBodies = false; awaiter.OnCompleted(moveNextDelegate); return; } } else { state = 0; } int awaitResult = awaiter.GetResult(); awaiter = new TaskAwaiter <int>(); value = awaitResult; writer.WriteLine("Got value {0}", value); sum += value; break; } catch (Exception) { Console.WriteLine("Oops... retrying"); } j++; } i++; } result = sum; } finally { if (doFinallyBodies && writer != null) { writer.Dispose(); } } } catch (Exception e) { state = -1; builder.SetException(e); return; } state = -1; builder.SetResult(result); }
public void MoveNext() { int result; try { int awaitResult1 = 0; int awaitResult2 = 0; int awaitResult3 = 0; #pragma warning disable 0219 // doFinallyBodies is never used bool doFinallyBodies = true; #pragma warning restore switch (state) { case 1: break; case 2: goto Label_Awaiter2Continuation; case 3: goto Label_Awaiter3Continuation; default: if (state != -1) { task1 = Task.Factory.StartNew(() => 1); task2 = Task.Factory.StartNew(() => 2); task3 = Task.Factory.StartNew(() => 3); awaiter1 = task1.GetAwaiter(); if (awaiter1.IsCompleted) { goto Label_GetAwaiter1Result; } state = 1; doFinallyBodies = false; awaiter1.OnCompleted(moveNextDelegate); } return; } state = 0; Label_GetAwaiter1Result: awaitResult1 = awaiter1.GetResult(); awaiter1 = new TaskAwaiter <int>(); awaiter2 = task2.GetAwaiter(); if (awaiter2.IsCompleted) { goto Label_GetAwaiter2Result; } state = 2; doFinallyBodies = false; awaiter2.OnCompleted(moveNextDelegate); return; Label_Awaiter2Continuation: state = 0; Label_GetAwaiter2Result: awaitResult2 = awaiter2.GetResult(); awaiter2 = new TaskAwaiter <int>(); awaiter3 = task3.GetAwaiter(); if (awaiter3.IsCompleted) { goto Label_GetAwaiter3Result; } state = 3; doFinallyBodies = false; awaiter3.OnCompleted(moveNextDelegate); return; Label_Awaiter3Continuation: state = 0; Label_GetAwaiter3Result: awaitResult3 = awaiter3.GetResult(); awaiter3 = new TaskAwaiter <int>(); result = awaitResult1 + awaitResult2 + awaitResult3; } catch (Exception e) { state = -1; builder.SetException(e); return; } state = -1; builder.SetResult(result); }