https://github.com/evandixon/skyeditor.utilities.asyncfor
Run the body of a For or a For Each loop asynchronously.
https://github.com/evandixon/skyeditor.utilities.asyncfor
async asynchronous dotnet each for foreach
Last synced: 4 months ago
JSON representation
Run the body of a For or a For Each loop asynchronously.
- Host: GitHub
- URL: https://github.com/evandixon/skyeditor.utilities.asyncfor
- Owner: evandixon
- License: mit
- Created: 2019-04-20T17:54:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-20T23:56:48.000Z (over 6 years ago)
- Last Synced: 2024-04-30T03:45:59.144Z (over 1 year ago)
- Topics: async, asynchronous, dotnet, each, for, foreach
- Language: C#
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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'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);
```