Skip to content

Instantly share code, notes, and snippets.

@Anderson0xFF
Forked from enghqii/Program.cs
Created September 7, 2020 13:00
Show Gist options
  • Select an option

  • Save Anderson0xFF/b63083ca2dc7380efd7282a52e5ae37e to your computer and use it in GitHub Desktop.

Select an option

Save Anderson0xFF/b63083ca2dc7380efd7282a52e5ae37e to your computer and use it in GitHub Desktop.
C# Coroutine example
using System;
using System.Collections;
using System.Collections.Generic;
namespace Generator3
{
class Program
{
// classic Generator
static IEnumerable<string> Script()
{
yield return "Their first meeting";
yield return "Over the last nine seasons of The Big Bang Theory, we've loved watching Sheldon and Leonard's bromance blossom, and their bond only seems to grow stronger as the years pass.";
yield return "Let's take a look back at the tumultuous—but always entertaining—friendship of these brainy pals.";
yield return "Although fans didn’t witness the guys' first meeting until Season 3 (Episode 22, \"The Staircase Implementation\"), we learned that Leonard was introduced to Sheldon after looking for a room to rent near Caltech. And while he was warned by former roommates and neighbors of the theoretical physicist that Sheldon was crazy, Leonard proceeded through the extensive interview process and roommate agreement before eventually moving in.";
yield return "The End";
yield break;
}
static void Main(string[] args)
{
Sched.Instance.StartCoroutine(Init());
while(true)
{
Update();
}
}
static void Update()
{
Sched.Instance.Update();
}
static IEnumerator Init()
{
foreach (string line in Script())
{
Console.WriteLine(line);
Console.WriteLine();
int sec = line.Split(' ').Length / 10 + 3;
Console.WriteLine("[Waiting for " + sec + " seconds]");
yield return Sched.Instance.StartCoroutine(WaitAboutSeconds(sec));
}
yield break;
}
static IEnumerator WaitAboutSeconds(int seconds)
{
// dumb timer
int timer = DateTime.Now.Second + seconds;
while (DateTime.Now.Second <= timer)
{
// pass
yield return null;
}
yield break;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Generator3
{
// Scheduler
class Sched
{
// simple singleton
private static Sched instance = new Sched();
public static Sched Instance
{
get { return instance; }
}
public class Coroutine
{
public IEnumerator routine;
public Coroutine waitForCoroutine;
public bool finished = false;
public Coroutine(IEnumerator routine) { this.routine = routine; }
}
List<Coroutine> coroutines = new List<Coroutine>();
private Sched() {}
public Coroutine StartCoroutine(IEnumerator routine)
{
Coroutine coroutine = new Coroutine(routine);
coroutines.Add(coroutine);
return coroutine;
}
public void Update()
{
foreach (Coroutine coroutine in coroutines.Reverse<Coroutine>())
{
if (coroutine.routine.Current is Coroutine)
coroutine.waitForCoroutine = coroutine.routine.Current as Coroutine;
if (coroutine.waitForCoroutine != null && coroutine.waitForCoroutine.finished)
coroutine.waitForCoroutine = null;
if (coroutine.waitForCoroutine != null)
continue;
// update coroutine
if (coroutine.routine.MoveNext())
{
coroutine.finished = false;
}
else
{
coroutines.Remove(coroutine);
coroutine.finished = true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment