Skip to content

Instantly share code, notes, and snippets.

@enghqii
Created August 29, 2016 18:15
Show Gist options
  • Save enghqii/cd989a3f4aad28e858015ed44259dd66 to your computer and use it in GitHub Desktop.
Save enghqii/cd989a3f4aad28e858015ed44259dd66 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;
}
}
}
}
}
@enghqii
Copy link
Author

enghqii commented Aug 29, 2016

I wanna sleep

@wrymn
Copy link

wrymn commented May 25, 2021

I wanna sleep

WTF 😅

@enghqii
Copy link
Author

enghqii commented May 26, 2021

@wrymn lol I forgot this gist even existed. At the moment, I was pulling an overnight overtime work. I left the comment at 3 am in the morning in the office. (btw, now I guess I should have said "I wanna go to bed")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment