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);

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.