Coroutine/Example/Example.cs

76 lines
3.2 KiB
C#
Raw Permalink Normal View History

2019-06-22 17:24:50 +02:00
using System;
using System.Collections.Generic;
using System.Threading;
using Coroutine;
namespace Example {
2019-06-22 17:24:50 +02:00
internal static class Example {
private static readonly Event TestEvent = new Event();
public static void Main() {
2023-02-23 19:04:16 +01:00
var seconds = CoroutineHandler.Start(Example.WaitSeconds(), "Awesome Waiting Coroutine");
CoroutineHandler.Start(Example.PrintEvery10Seconds(seconds));
2019-06-22 17:24:50 +02:00
2023-02-23 19:04:16 +01:00
CoroutineHandler.Start(Example.EmptyCoroutine());
CoroutineHandler.InvokeLater(new Wait(5), () => {
Console.WriteLine("Raising test event");
2023-02-23 19:04:16 +01:00
CoroutineHandler.RaiseEvent(Example.TestEvent);
});
2023-02-23 19:04:16 +01:00
CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("Example event received"));
2023-02-23 19:04:16 +01:00
CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("I am invoked after 'Example event received'"), priority: -5);
CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("I am invoked before 'Example event received'"), priority: 2);
2021-03-16 19:07:28 +01:00
2019-06-22 17:24:50 +02:00
var lastTime = DateTime.Now;
while (true) {
var currTime = DateTime.Now;
CoroutineHandler.Tick(currTime - lastTime);
2019-06-22 17:24:50 +02:00
lastTime = currTime;
Thread.Sleep(1);
}
}
private static IEnumerator<Wait> WaitSeconds() {
2019-06-22 17:24:50 +02:00
Console.WriteLine("First thing " + DateTime.Now);
yield return new Wait(1);
2019-06-22 17:24:50 +02:00
Console.WriteLine("After 1 second " + DateTime.Now);
yield return new Wait(9);
2019-06-22 17:24:50 +02:00
Console.WriteLine("After 10 seconds " + DateTime.Now);
2023-02-23 19:04:16 +01:00
CoroutineHandler.Start(Example.NestedCoroutine());
yield return new Wait(5);
Console.WriteLine("After 5 more seconds " + DateTime.Now);
yield return new Wait(10);
Console.WriteLine("After 10 more seconds " + DateTime.Now);
2019-06-22 17:24:50 +02:00
yield return new Wait(20);
Console.WriteLine("First coroutine done");
2019-06-22 17:24:50 +02:00
}
private static IEnumerator<Wait> PrintEvery10Seconds(ActiveCoroutine first) {
2019-06-22 17:24:50 +02:00
while (true) {
yield return new Wait(10);
2019-06-22 17:24:50 +02:00
Console.WriteLine("The time is " + DateTime.Now);
if (first.IsFinished) {
Console.WriteLine("By the way, the first coroutine has finished!");
Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " +
2021-03-16 19:07:28 +01:00
$"{first.TotalMoveNextTime.TotalMilliseconds} total time, " +
$"{first.LastMoveNextTime.TotalMilliseconds} last time");
Environment.Exit(0);
}
2019-06-22 17:24:50 +02:00
}
}
private static IEnumerator<Wait> EmptyCoroutine() {
yield break;
}
private static IEnumerable<Wait> NestedCoroutine() {
Console.WriteLine("I'm a coroutine that was started from another coroutine!");
yield return new Wait(5);
Console.WriteLine("It's been 5 seconds since a nested coroutine was started, yay!");
}
2019-06-22 17:24:50 +02:00
}
}