Ellpeck / Coroutine

A simple implementation of Unity's Coroutines to be used for any C# project
https://www.nuget.org/packages/Coroutine
MIT License
37 stars 3 forks source link

how to interrupt the current coroutine (and delete the entire queue) #17

Closed Divertisment closed 1 year ago

Divertisment commented 1 year ago

hi, I like your library very much. I sketched an example. Is it possible to somehow interrupt the execution of the current coroutine and proceed to the execution of the next one. Or interrupt the current one and delete the entire queue. The essence of the issue is stated in the code - if we press Backspace and Del

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Coroutine;
namespace Stas.Test.Native {
    internal class RutineTest {
        Event e0 { get; } = new();
        Event e1 { get; } = new();
        Event e2 { get; } = new();
        Event e3 { get; } = new();
        Event e4 { get; } = new();
        Event e5 { get; } = new();
        Event e6 { get; } = new();
        Event e7 { get; } = new();
        Event e8 { get; } = new();
        Event e9 { get; } = new();

        public RutineTest() {
            Console.WriteLine("press 0-9 same time");
            Console.WriteLine("press Del for interapt all routine");
            CoroutineHandler.Start(zero());
            CoroutineHandler.Start(one());
            CoroutineHandler.Start(two());
            CoroutineHandler.Start(three());
            CoroutineHandler.Start(@for(), "@for", int.MaxValue);
            CoroutineHandler.Start(five());
            CoroutineHandler.Start(six());
            CoroutineHandler.Start(seven());
            CoroutineHandler.Start(eight());
            CoroutineHandler.Start(nine());
            var lastTime = DateTime.Now;
            while (true) {
                var currTime = DateTime.Now;
                CoroutineHandler.Tick(currTime - lastTime);
                lastTime = currTime;
                GetCommand();
                Thread.Sleep(1);
            }
            void GetCommand () {
                var k  =Console.ReadKey().Key;
                var curr=Console.GetCursorPosition();
                Console.SetCursorPosition(curr.Left - 1, curr.Top);
                Console.Write(" ");
                switch (k) {
                    case ConsoleKey.Backspace:
                        //TODO: hoow to interapt current corutine?
                        // StopCurrCoroutine().
                        break;
                    case ConsoleKey.Delete:
                        //TODO: hoow to interapt current corutine and clareing Queue?
                        // StopCurrAndCleareQueue()
                        break;
                    case ConsoleKey.D0:
                        CoroutineHandler.RaiseEvent(e0);
                        break;
                    case ConsoleKey.D1:
                        CoroutineHandler.RaiseEvent(e1);
                        break;
                    case ConsoleKey.D2:
                        CoroutineHandler.RaiseEvent(e2);
                        break;
                    case ConsoleKey.D3:
                        CoroutineHandler.RaiseEvent(e3);
                        break;
                    case ConsoleKey.D4:
                        CoroutineHandler.RaiseEvent(e4);
                        break;
                    case ConsoleKey.D5:
                        CoroutineHandler.RaiseEvent(e5);
                        break;
                    case ConsoleKey.D6:
                        CoroutineHandler.RaiseEvent(e6);
                        break;
                    case ConsoleKey.D7:
                        CoroutineHandler.RaiseEvent(e7);
                        break;
                    case ConsoleKey.D8:
                        CoroutineHandler.RaiseEvent(e8);
                        break;
                    case ConsoleKey.D9:
                        CoroutineHandler.RaiseEvent(e9);
                        break;
                    default:
                        Console.WriteLine("["+ k + "]=>no action");
                        break;
                }
            }
        }
        Random R = new();
        int min = 10;
        int max = 40;
        IEnumerator<Wait> zero() {
            while (true) {
                yield return new Wait(e0);
                Console.Write("\nzero started");
                for (int i = 0; i < R.Next(min, max); i++) { 
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> one() {
            while (true) {
                yield return new Wait(e1);
                Console.Write("\none started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> two() {
            while (true) {
                yield return new Wait(e2);
                Console.Write("\ntwo started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> three() {
            while (true) {
                yield return new Wait(e3);
                Console.Write("\nthree started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> @for() {
            while (true) {
                yield return new Wait(e4);
                Console.Write("\nfor started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> five() {
            while (true) {
                yield return new Wait(e5);
                Console.Write("\nfive started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> six() {
            while (true) {
                yield return new Wait(e6);
                Console.Write("\nsix started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> seven() {
            while (true) {
                yield return new Wait(e7);
                Console.Write("\nseven started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> eight() {
            while (true) {
                yield return new Wait(e8);
                Console.Write("\neight started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }
        IEnumerator<Wait> nine() {
            while (true) {
                yield return new Wait(e9);
                Console.Write("\nnine started");
                for (int i = 0; i < R.Next(min, max); i++) {
                    Thread.Sleep(R.Next(10, 200));
                    Console.Write(".");
                }
                Console.Write("end");
            }
        }

    }
}
Ellpeck commented 1 year ago

Hi! I'm not sure what you mean by "clearing the queue", but you can cancel a Coroutine using the Cancel method. I hope that answers your question!

Divertisment commented 1 year ago

And the second question. Priority doesn't work for me. If I press 1 2 3 4 the tasks will be executed exactly in that order. But since I set 4 as the maximum priority the queue should be executed 1423 - it would be logical.

Divertisment commented 1 year ago

"clearing the queue"

Pls just run my example and pres 1 2 3 4 and see what happens. We will get a queue of tasks 1 2 34

Ellpeck commented 1 year ago

Priority is about the order that coroutine steps will be executed when they're invoked in the same Tick or RaiseEvent call.

To make an example, if you were to raise an event that four different coroutines are waiting on, priority dictates what order their actions are invoked in. If you raise four different events "at the same time" (which is really just four different events raised in order), and four coroutines are waiting for one of those events each, priority has no effect.

Ellpeck commented 1 year ago

For more insight into how various parts of the library are meant to work in greater detail, I recommend checking out the tests :)

Divertisment commented 1 year ago

Priority is about the order that coroutine steps will be executed when they're invoked in the same Tick or RaiseEvent call.

ah ok, i see. thanks

Divertisment commented 1 year ago

For more insight into how various parts of the library are meant to work in greater detail, I recommend checking out the tests :)

I'll close it as soon as I figure it out if you don't mind. Thanks