2019-06-22 17:24:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Coroutine;
|
|
|
|
|
|
|
|
|
|
namespace Test {
|
|
|
|
|
internal static class Example {
|
|
|
|
|
|
|
|
|
|
private static readonly Event TestEvent = new Event();
|
|
|
|
|
|
|
|
|
|
public static void Main() {
|
2020-02-28 22:27:38 +01:00
|
|
|
|
var seconds = CoroutineHandler.Start(WaitSeconds());
|
|
|
|
|
CoroutineHandler.Start(PrintEvery10Seconds(seconds));
|
2019-06-22 17:24:50 +02:00
|
|
|
|
|
2019-11-20 11:30:28 +01:00
|
|
|
|
CoroutineHandler.InvokeLater(new WaitSeconds(10), () => {
|
|
|
|
|
Console.WriteLine("Raising test event");
|
|
|
|
|
CoroutineHandler.RaiseEvent(TestEvent);
|
|
|
|
|
});
|
|
|
|
|
CoroutineHandler.InvokeLater(new WaitEvent(TestEvent), () => Console.WriteLine("Test event received"));
|
|
|
|
|
|
2019-06-22 17:24:50 +02:00
|
|
|
|
var lastTime = DateTime.Now;
|
|
|
|
|
while (true) {
|
|
|
|
|
var currTime = DateTime.Now;
|
|
|
|
|
CoroutineHandler.Tick((currTime - lastTime).TotalSeconds);
|
|
|
|
|
lastTime = currTime;
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 11:30:28 +01:00
|
|
|
|
private static IEnumerator<IWait> WaitSeconds() {
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("First thing " + DateTime.Now);
|
|
|
|
|
yield return new WaitSeconds(1);
|
|
|
|
|
Console.WriteLine("After 1 second " + DateTime.Now);
|
2019-11-20 11:30:28 +01:00
|
|
|
|
yield return new WaitSeconds(9);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("After 10 seconds " + DateTime.Now);
|
2019-11-20 11:30:28 +01:00
|
|
|
|
yield return new WaitSeconds(5);
|
|
|
|
|
Console.WriteLine("After 5 more seconds " + DateTime.Now);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
yield return new WaitSeconds(10);
|
2019-11-20 11:30:28 +01:00
|
|
|
|
Console.WriteLine("After 10 more seconds " + DateTime.Now);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
|
2019-11-20 11:30:28 +01:00
|
|
|
|
yield return new WaitSeconds(20);
|
2020-02-28 22:27:38 +01:00
|
|
|
|
Console.WriteLine("First coroutine done");
|
2019-06-22 17:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 22:27:38 +01:00
|
|
|
|
private static IEnumerator<IWait> PrintEvery10Seconds(ActiveCoroutine first) {
|
2019-06-22 17:24:50 +02:00
|
|
|
|
while (true) {
|
|
|
|
|
yield return new WaitSeconds(10);
|
|
|
|
|
Console.WriteLine("The time is " + DateTime.Now);
|
2020-02-28 22:27:38 +01:00
|
|
|
|
if (first.IsFinished) {
|
|
|
|
|
Console.WriteLine("By the way, the first coroutine has finished!");
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
}
|
2019-06-22 17:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|