System Library Common Framework
Search Results for

    *.Benchmark*,*.Tests*,*.ApiTests*,*.BaseTest*,DelegateJsonConverter,CalleeCancelledRequestException,ClientResponse,ContentType,OutputCachePolicy
    Show / Hide Table of Contents Show / Hide Table of Contents
    NamespaceSystemLibrary.Common.Framework SystemLibrary.Common.Framework.Net.dll

    Run methods asynchronously without blocking.

    Async

    public static class Async

    Inheritance
    object
    Async
    Examples
    Arguments
    X

    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 exceptions
    Remarks
    X

    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)

    X
    Async.FireAndForget(() => System.IO.File.AppenAllText("C:\temp\text.log", "hello world"));
    X
    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 exceptions
    Remarks
    X

    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)

    X
    Async.FireAndForget((ex) => Log.Error(ex), () => System.IO.File.AppenAllText("C:\temp\text.log", "hello world"));
    X
    Methods arguments
    Type Name Description
    Action<Exception> onError

    Callback invoked if an exception occured

    Action[] actions

    Array of methods to invoke in a non-blocking way

    Execute methods in paralell manner where each result is appended to a List

    Halts execution till all functions passed have ran till completion

    Remarks
    X

    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

    public static List<T> Parallel<T>(params Func<T>[] functions)

    X
    var responses = Async.Parallel<string>(
        () => GetRedCars(url),
        () => GetBlueCars(url),
        () => GetGreenCars(url)
    );
    X
    Generic types
    Name Description
    T
    Methods arguments
    Type Name Description
    Func<T>[] functions
    X
    Type Description
    List<T>

    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)

    X
    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'
    X
    Generic types
    Name Description
    T
    Methods arguments
    Type Name Description
    Func<T>[] functions
    X
    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)

    X
    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'
    X
    Generic types
    Name Description
    T
    Methods arguments
    Type Name Description
    int timeoutMilliseconds
    Func<T>[] functions
    X
    Type Description
    List<T>

    }

    In This Article
    Package: nuget
    Source: github
    Website: System Library Common Framework