NamespaceSystemLibrary.Common.Net SystemLibrary.Common.Net.dll
Contains methods for running functions in a non-blocking way
NOTE: Might be removed at a later point in time or rewritten/restructured completely
Async
public static class Async
Inheritance
Examples
Arguments
Methods
Run all actions seperately in a non-blocking way
Once FireAndForget() is invoked, your code continues executing, as all actions passed runs in a non-blocking way, in an unordered way. Actions passed to the method will run till completion or till main thread is shutdown
Each action passed is ran in a try-catch without 'ever' crashing the callee.
See the overloaded method if you want to handle exceptions that might occur running your 'actions'
public static void FireAndForget(params Action[] actions)
Async.FireAndForget(() => System.IO.File.AppenAllText("C:\temp\text.log", "hello world"));
Methods arguments
Type | Name | Description |
---|---|---|
System.Action[] | actions | Fire and forget actions... |
Run all actions seperately in a non-blocking way
Once FireAndForget() is invoked, your code continues executing, as all actions passed runs in a non-blocking way, in an unordered way. Actions passed to the method will run till completion or till main thread is shutdown
Each action passed is ran in a try-catch without 'ever' crashing the callee.
- Pass in onError callback which executes if any exception is thrown
public static void FireAndForget(Action<Exception> onError, params Action[] actions)
Async.FireAndForget((ex) => Log.Error(ex), () => System.IO.File.AppenAllText("C:\temp\text.log", "hello world"));
Methods arguments
Type | Name | Description |
---|---|---|
System.Action<System.Exception> | onError | |
System.Action[] | actions |
Execute methods in a async manner, appending each single result to a list, and it halts execution till all functions passed as params has completed
public static List<T> Run<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> Get(string name) {
//HttpBaseClient exists in nuget package: SystemLibrary.Common.Web
return HttpBaseClient.Get<List<Car>>("https://systemlibrary.com/cars/q=?" + name);
}
}
var cars = Async.Run<Car>(
() => new CarApi().GetTop1("ferrari"),
() => new CarApi().GetTop1("volvo"),
() => new CarApi().GetTop1("tesla")
);
// Variable 'cars' is filled after all three api requests has completed.
// Assume we got 1 ferrari, 0 volvo and 1 tesla
// 'cars' now contain a total of 2 objects of type 'Car'
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Func<T>[] | functions |
Type | Description |
---|---|
System.Collections.Generic.List<T> |
Execute methods in a async manner, appending the range of results per function to the same list, and it halts execution till all functions passed as params has completed
public static List<T> Run<T>(params Func<IEnumerable<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> Get(string name) {
//HttpBaseClient exists in nuget package: SystemLibrary.Common.Web
return HttpBaseClient.Get<List<Car>>("https://systemlibrary.com/cars/q=?" + name);
}
}
var cars = Async.Run<Car>(
() => new CarApi().GetAll("ferrari"),
() => new CarApi().GetAll("volvo"),
() => new CarApi().GetAll("tesla")
);
// Variable 'cars' is filled after all three api requests has completed.
// Assume we got 2 ferraris, 3 volvos and 4 teslas
// 'cars' now contain a total of 9 objects of type 'Car'
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Func<System.Collections.Generic.IEnumerable<T>>[] | functions |
Type | Description |
---|---|
System.Collections.Generic.List<T> |
}