Giter Club home page Giter Club logo

debouncethrottle's Introduction

C# Debounce and Throttle dispatchers

Debounce and Throttle dispatchers support asynchronous actions, handle exceptions. All Task results from dispatcher calls will be equal to result of the a single invoking.

Debounce demo

Show entered text after stopping pressing keys for 1000 milliseconds

    class Program
    {
        static void Main(string[] args)
        {
            string str = "";
            var debounceDispatcher = new DebounceDispatcher(1000);
            while(true)
            {
                var key = Console.ReadKey(true);

                //trigger when to stop and exit
                if (key.Key == ConsoleKey.Escape) break;

                str += key.KeyChar;

                //every keypress iteration call dispatcher but the Action will be invoked only after stopping pressing and waiting 1000 milliseconds
                debounceDispatcher.Debounce(() =>
                {
                    Console.WriteLine($"{str} - {DateTime.UtcNow.ToString("hh:mm:ss.fff")}");
                    str = "";
                });
            }
        }
    }

Throttle demo

Call action every 100 milliseconds but invoke only once in 1000 milliseconds (after last invoking completed)

    class Program
    {
        static void Main(string[] args)
        {
            bool stop = false;
            //trigger when to stop and exit
            Task.Run(() => { Console.ReadKey(true); stop = true; });

            var throttleDispatcher = new ThrottleDispatcher(1000);
            do
            {
                //every iteration call dispatcher but the Action will be invoked only once in 1500 milliseconds (500 action work time + 1000 interval)
                throttleDispatcher.ThrottleAsync(async () =>
                {
                    Console.WriteLine($"{ DateTime.UtcNow.ToString("hh:mm:ss.fff") }");
                    await Task.Delay(500);
                });

                //iteration every 100 milliseconds
                Thread.Sleep(100);
            }
            while (!stop); //wait trigger to stop and exit
        }
    }

debouncethrottle's People

Contributors

coddicat avatar solomonrodion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.