NamespaceSystemLibrary.Common.Framework SystemLibrary.Common.Framework.Net.dll
Run methods asynchronously without blocking.
Async
public static class Async
Inheritance
Examples
Arguments
Methods
Run all actions seperately in a non-blocking way
Each action passed is ran in a try catch without notifying callee
See the overloaded method to add a callback for logging exceptionsRemarks
All functions passed to this is ran in an unordered and non-blocking way
All functions passed will run till completion, erroring or till main thread is shut down
public static void FireAndForget(params Action[] actions)
Async.FireAndForget(() => System.IO.File.AppenAllText("C:\temp\text.log", "hello world"));
Methods arguments
Type | Name | Description |
---|---|---|
Action[] | actions | Array of methods to invoke in a non-blocking way |
Run all actions seperately in a non-blocking way
Each action passed is ran in a try catch without notifying callee
See the overloaded method if you want to ignore exceptionsRemarks
All functions passed to this is ran in an unordered and non-blocking way
All functions passed will run till completion, erroring or till main thread is shut down
public static void FireAndForget(Action<Exception> onError, params Action[] actions)
Execute methods in paralell manner where each result is appended to a List
Halts execution till all functions passed have ran till completion
Remarks
Parallel swallows exceptions if the methods passed does throw
Parallel requires the methods pass to always return data
Null values returned from the methods will be filtered away
Parallel usage is decent when you: do something towards local file system, file searches or CPU bound calculations
Execute methods asynchronously, appending each result to a list, and halt execution until all functions passed as parameters have completed.
A hardcoded 30-second limit for all methods to complete; if exceeded, some results may be omitted, an error is logged, but the data collected so far is returned.
public static List<T> Tasks<T>(params Func<T>[] functions)
class Car {
public string Name { get; set; }
}
class CarApi {
//Simple dummy method that pretends to return a list of cars based on the name from some API
List<Car> GetByName(string name) {
//Client exists in nuget package: SystemLibrary.Common.Framework.App
return Client.Get<List<Car>>("https://systemlibrary.com/cars/q=?" + name);
}
}
var carApi = new CarApi();
var cars = Async.Tasks<Car>(
() => carApi.GetByName("blue"),
() => carApi.GetByName("red"),
() => carApi.GetByName("orange")
);
// Variable 'cars' is filled after all three api requests has completed.
// Assume we got 1 blue, 0 red and 1 orange
// 'cars' now contain a total of 2 objects of type 'Car'
Type | Description |
---|---|
List<T> |
Execute methods in an async manner, appending each single result to a list, and it halts execution till all functions passed as params has completed
Pass in a specific timeout limit in milliseconds, before the results will be returned 'as is'
public static List<T> Tasks<T>(int timeoutMilliseconds, params Func<T>[] functions)
class Car {
public string Name { get; set; }
}
class CarApi {
//Simple dummy method that pretends to return a list of cars based on the name from some API
List<Car> GetByName(string name) {
//Client exists in nuget package: SystemLibrary.Common.Framework.App
return Client.Get<List<Car>>("https://systemlibrary.com/cars/q=?" + name);
}
}
var carApi = new CarApi();
var cars = Async.Tasks<Car>(7500,
() => carApi.GetByName("blue"),
() => carApi.GetByName("red"),
() => carApi.GetByName("orange")
);
// Variable 'cars' is filled after all three api requests has completed.
// Assume we got 1 blue, 0 red and 1 orange
// 'cars' now contain a total of 2 objects of type 'Car'
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
int | timeoutMilliseconds | |
Func<T>[] | functions |
Type | Description |
---|---|
List<T> |
}