Giter VIP home page Giter VIP logo

skyeditor.utilities.asyncfor's Introduction

SkyEditor.Utilities.AsyncFor

Run the body of a for or a foreach loop asynchronously.

Features:

  • Asynchronous for-style loops, with optional custom step count
  • Asynchronous foreach-style loops
  • Monitor progress through events or progress report token
  • Optionally run all tasks one at a time, useful in scenarios where race conditions are present but you still want to monitor progress
  • Set maximum number of concurrent tasks

Examples

for

// Basic Example: print numbers 0 through 10 (inclusive)
await AsyncFor.For(0, 10, i =>
{
    Console.WriteLine(i);
});

// Custom step count: print only the even numbers from 0 through 10 (inclusive)
await AsyncFor.For(0, 10, i =>
{
    Console.WriteLine(i);
}, stepCount: 2);

foreach

var sampleData = new[] { 0, 1, 2, 3 };

// Basic usage: print all numbers in the array
await AsyncFor.ForEach(sampleData, data => {
    Console.WriteLine(data);
});

// Extension method: available for all IEnumerable's and IEnumerable<T>'s
await sampleData.RunAsyncForEach(data => {
    Console.WriteLine(data);
});

Concurrency options

// Prevent more than 3 tasks from running concurrently
await AsyncFor.For(0, 10, i =>
{
    Console.WriteLine(i);
}, batchSize: 3);

// Run tasks one at a time
await AsyncFor.For(0, 10, i =>
{
    Console.WriteLine(i);
}, runSynchronously: true);

See progress

var progressToken = new ProgressReportToken();
progressToken.ProgressChanged += (object sender, ProgressReportedEventArgs e) =>
{
    Console.WriteLine($"Progress: {e.Progress * 100} %");
};
progressToken.Completed += (object sender, EventArgs e) =>
{
    Console.WriteLine("Completed!");
};

await AsyncFor.For(0, 10, i =>
{
    Console.WriteLine(i);
}, progressReportToken: progressToken);

skyeditor.utilities.asyncfor's People

Contributors

evandixon avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

skyeditor.utilities.asyncfor's Issues

Deprecate AsyncFor instance methods

When the AsyncFor was first made, it consisted only of instance members, with no static functions. Static functions were later added for ease of use, and today, the static functions are the preferred method of execution, so much so that they're the only examples shown in the readme. The instance members cannot currently be removed because other Sky Editor code relies on it. To make it easier to move forward, the old code needs to be marked obsolete.

Acceptance Criteria:

  • The AsyncFor (and AsyncForEach) code is moved to the static functions
  • The instance functions are not removed, but are modified to call the static functions
  • Where appropriate, the instance properties wrap single instance of a ProgressReportToken
  • All instance members are marked with an Obsolete attribute
  • Upon completion of this issue, another issue is made that says to remove the instance members entirely and make the class a static class

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.