// ------------------ COMPLEX EXAMPLE ------------------ private static async void ComplexEntryPoint(Coordinator coordinator) { await coordinator.Execute(SetUpLogComeFrom); int count = 0; await coordinator.ComeFrom("End"); Console.WriteLine("Just after ComeFrom(End). count={0}", count); if (count == 5) { Console.WriteLine("Okay, we're done now"); return; } count++; await coordinator.Label("Log"); await coordinator.ComeFrom("Middle"); Console.WriteLine("Just after ComeFrom(Middle). count={0}", count); if (count < 3) { await coordinator.Label("TimestampedLog"); count++; await coordinator.Label("Middle"); } await coordinator.Label("End"); }
private static async Task<string> SecondCoroutine( Coordinator<string> coordinator, string initialValue) { Console.WriteLine(" Starting SecondCoroutine with initial value {0}", initialValue); Console.WriteLine(" Starting SecondCoroutine"); Console.WriteLine(" Yielding 'y1' from SecondCoroutine..."); string received = await coordinator.Yield("y1"); Console.WriteLine(" Returned to SecondCoroutine with value {0}", received); Console.WriteLine(" Yielding 'y2' from SecondCoroutine..."); received = await coordinator.Yield("y2"); Console.WriteLine(" Returned to SecondCoroutine with value {0}", received); // FirstCoroutine has finished now - so we'll get y3 back to ourselves Console.WriteLine(" Yielding 'y3' from SecondCoroutine..."); received = await coordinator.Yield("y3"); Console.WriteLine(" Returned to SecondCoroutine with value {0}", received); Console.WriteLine(" Finished SecondCoroutine"); return "y4"; }
private static void Main(string[] args) { var coordinator = new Coordinator<string>(FirstCoroutine, SecondCoroutine, ThirdCoroutine); string finalResult = coordinator.Start("m1"); Console.WriteLine("Final result: {0}", finalResult); }
private static void Main(string[] args) { var coordinator = new Coordinator { FirstCoroutine, SecondCoroutine, ThirdCoroutine }; coordinator.Start(); }
private static async void ThirdCoroutine(Coordinator coordinator) { Console.WriteLine(" Starting ThirdCoroutine"); Console.WriteLine(" Yielding from ThirdCoroutine..."); await coordinator; Console.WriteLine(" Returned to ThirdCoroutine"); Console.WriteLine(" Finished ThirdCoroutine..."); }
private static async void RegisterForFirstLabel(Coordinator coordinator) { Console.WriteLine("Registering interest"); await coordinator.ComeFrom("FirstLabel"); Console.WriteLine("Looks like the first label was hit! (Let's hit it again.)"); await coordinator.Label("FirstLabel"); Console.WriteLine("Done with the first label now"); }
private static void Main(string[] args) { var coordinator = new Coordinator(); for (int i = 0; i < 5; i++) { // Avoid capturing the index variable int copy = i; coordinator.Add(x => CreateCoroutine(copy, x)); }; coordinator.Start(3); }
private static async void SimpleOtherMethod(Coordinator coordinator) { Console.WriteLine("Start of SimpleOtherMethod"); int count = 0; await coordinator.ComeFrom("x"); Console.WriteLine("After ComeFrom x in SimpleOtherMethod. count={0}. Returning.", count); count++; }
private static async void EntryPoint(Coordinator coordinator) { RegisterForFirstLabel(coordinator); RegisterForFirstLabelAgain(coordinator); RegisterForSecondLabel(coordinator); Console.WriteLine("Hitting first label..."); await coordinator.Label("FirstLabel"); Console.WriteLine("Hitting second label..."); await coordinator.Label("SecondLabel"); }
private static async Task<string> ThirdCoroutine( Coordinator<string> coordinator, string initialValue) { Console.WriteLine(" Starting ThirdCoroutine with initial value {0}", initialValue); Console.WriteLine(" Yielding 'z1' from ThirdCoroutine..."); string received = await coordinator.Yield("z1"); Console.WriteLine(" Returned to ThirdCoroutine with value {0}", received); Console.WriteLine(" Finished ThirdCoroutine..."); return "z2"; }
private static async void Consumer(Coordinator coordinator) { int loopCount = 0; while (true) { Console.WriteLine(" Entering consumer loop; count {0}", loopCount); loopCount++; while (values.Count != 0) { Console.WriteLine(" Dequeued {0}", values.Dequeue()); } Console.WriteLine(" Queue empty; switching to producer"); await coordinator.YieldTo(Producer); } }
private static async void Producer(Coordinator coordinator) { for (int i = 0; i < 10; i++) { Console.WriteLine("Enqueuing {0}", i); values.Enqueue(i); if (values.Count == 3) { Console.WriteLine("Queue full; switching to consumer"); await coordinator.YieldTo(Consumer); } } // Dequeue any remaining items await coordinator.YieldTo(Consumer); }
private static async void SecondCoroutine(Coordinator coordinator) { Console.WriteLine(" Starting SecondCoroutine"); Console.WriteLine(" Yielding from SecondCoroutine..."); await coordinator; Console.WriteLine(" Returned to SecondCoroutine"); Console.WriteLine(" Yielding from SecondCoroutine again..."); await coordinator; Console.WriteLine(" Returned to SecondCoroutine again"); Console.WriteLine(" Finished SecondCoroutine"); }
private static async void EntryPoint(Coordinator coordinator) { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { Console.WriteLine("Gosub HandleEven"); await coordinator.Gosub(HandleEven, i); } else { Console.WriteLine("Gosub HandleEven"); await coordinator.Gosub(HandleOdd, i); } } }
private static async Task<string> FirstCoroutine( Coordinator<string> coordinator, string initialValue) { Console.WriteLine("Starting FirstCoroutine with initial value {0}", initialValue); Console.WriteLine("Yielding 'x1' from FirstCoroutine..."); string received = await coordinator.Yield("x1"); Console.WriteLine("Returned to FirstCoroutine with value {0}", received); Console.WriteLine("Yielding 'x2' from FirstCoroutine..."); received = await coordinator.Yield("x2"); Console.WriteLine("Returned to FirstCoroutine with value {0}", received); Console.WriteLine("Finished FirstCoroutine"); return "x3"; }
private static async void CreateCoroutine(int index, Coordinator coordinator) { Console.WriteLine("Starting Coroutine {0} on thread {1}", index, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(200); Console.WriteLine("Coroutine {0} yielding", index); await coordinator; Console.WriteLine("Coroutine {0} continuing on thread {1}", index, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(200); Console.WriteLine("Coroutine {0} yielding", index); await coordinator; Console.WriteLine("Coroutine {0} continuing (again) on thread {1}", index, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(200); Console.WriteLine("Coroutine {0} exiting", index); }
// ------------------ SIMPLE EXAMPLE ------------------ private static async void SimpleEntryPoint(Coordinator coordinator) { await coordinator.Execute(SimpleOtherMethod); Console.WriteLine("First call to Label(x)"); await coordinator.Label("x"); Console.WriteLine("Second call to Label(x)"); await coordinator.Label("x"); Console.WriteLine("Registering interesting in y"); bool firstTime = true; await coordinator.ComeFrom("y"); Console.WriteLine("After ComeFrom(y). FirstTime={0}", firstTime); if (firstTime) { firstTime = false; await coordinator.Label("y"); } Console.WriteLine("Finished"); }
internal ExecuteAwaiter(Action action, Coordinator coordinator) { this.action = action; this.coordinator = coordinator; }
internal ComeFromAwaiter(string label, Coordinator coordinator) { this.label = label; this.coordinator = coordinator; }
internal LabelAwaiter(Continuation continuation, Coordinator coordinator) { this.continuation = continuation; this.coordinator = coordinator; }
internal LabelAwaiter(Coordinator coordinator, Action pendingAction) { this.pendingAction = pendingAction; this.coordinator = coordinator; }
private static async void HandleOdd(Coordinator coordinator, int value) { await coordinator.Gosub(Print, value + " is odd"); }
internal Awaiter(Coordinator coordinator, Action targetAction) { this.coordinator = coordinator; this.targetAction = targetAction; }
private static async void HandleEven(Coordinator coordinator, int value) { await coordinator.Gosub(Print, value + " is even"); }
private static async void SetUpLogComeFrom(Coordinator coordinator) { await coordinator.ComeFrom("TimestampedLog"); Console.WriteLine("Message at {0}", DateTime.Now); // It would be nice to be able to pass a value, but that's tricky // for various reasons. await coordinator.ComeFrom("Log"); Console.WriteLine("We logged something."); }
private static async void RegisterForSecondLabel(Coordinator coordinator) { await coordinator.ComeFrom("SecondLabel"); Console.WriteLine("Second label hit too"); }
private static void Main(string[] args) { Coordinator coordinator = new Coordinator(EntryPoint); coordinator.Start(); }
private static async void RegisterForFirstLabelAgain(Coordinator coordinator) { await coordinator.ComeFrom("FirstLabel"); Console.WriteLine("Looks like the first label was hit a second time!"); }
private static void Main(string[] args) { Coordinator coordinator = new Coordinator(); coordinator.Start(Producer); }