Kelson.Common

Collection of packages published under the Kelson.Common namespace

View the Project on GitHub KelsonBall/Kelson.Common

Piped Dispatch

Extension methods are provided to allow for easy dispatch of ‘continue with’ callbacks to specific threads. They return void because the thread that is dispatched to becomes responsible for the task and its exceptions.

Concept example:

// "I have an asynchronous IO operation and when its done I need to update the UI on the UI thread"
asyncRepository.GetThingAsync().Then(result => UiElement.Value = result);

// "I have some asynchronous stuff to do and want its exceptions to be thrown in this context"
asyncRepository.DoThingAsync().Confirm();

RegisterDispatchCallback(Action<Action> dispatcher, int dispatcherHandle = 0)

// using Kelson.Common.Async;

// Register the UI thread dispatcher of a WPF app on handle 0
StaticDispatch.RegisterCallbackDispatcher(a => App.Current.Dispatcher.Invoke(a));

// Register a dispatcher to a Task on handle 1
var myTask = Task.Run(() => {});
StaticDispatch.RegisterCallbackDispatcher(a => myTask.ContinueWith(t => a()), dispatcherHandle : 1);

RegisterExceptionLogger(Action<Exception> handler)

// using Kelson.Common.Async;

StaticDispatch.RegisterExceptionLogger(e => Console.WriteLine(e.Message));

Task.Run(() => throw new Exception("Expected exception")).Then(() => Console.WriteLine("Done"));
> Expected exception

Then(this Task task, Action action, int dispatcherHandle = 0)

// using Kelson.Common.Async;

Task.Run(async () => await Task.Delay(1000))
    .Then(() => Console.WriteLine("Writing from thread with dispatcher 0"));
> Writing from thread with dispatcher 0

Task.Run(async () => await Task.Delay(1000))
    .Then(() => Console.WriteLine("Writing from thread with dispatcher 1"), dispatcherHandle : 1);
> Writing from thread with dispatcher 1

Then<T>(this Task<T> task, Action<T> action, int dispatcherHandle = 0)

// using Kelson.Common.Async;

Task.Run(async () => 1)
    .Then(v => Console.WriteLine($"Informing you from thread dispatched by dispatcher 0 that the task finished with value {v}"));
> Informing you from thread dispatched by dispatcher 0 that the task finished with value 1

Task.Run(async () => 2)
    .Then(v => Console.WriteLine($"Informing you from thread dispatched by dispatcher 1 that the task finished with value {v}"), dispatcherHandle : 1);
> Informing you from thread dispatched by dispatcher 1 that the task finished with value 2

Confirm(this Task task, int dispatchHandle = 0, Action successCallback = null, Action<Exception> errorCallback = null, Action cancelCallback = null)

// using Kelson.Common.Async;

Task.Run(() => {}).Confirm();

Task.Run(() => throw new Exception("Expected")).Confirm();
// exception gets thrown in the thread that invoked `Confirm`

Task.Run(() => {}).Confirm(successCallback: () => Console.WriteLine("Success!"))
> Success!

Task.Run(() => {}).Confirm(successCallback: () => Console.WriteLine("Success!"),
                           errorCallback: e => Console.WriteLine(e.Message)),
                           cancelCallback: () => Console.WriteLine("This task pulled a firefly"));
> Success!

Confirm<T>(this Task<T> task, int dispatchHandle = 0, Action<T> successCallback = null, Action<Exception> errorCallback = null, Action cancelCallback = null)

// using Kelson.Common.Async;

Task.Run(() => 2).Confirm();

Task.Run(() => throw new Exception("Expected")).Confirm();
// exception gets thrown in the thread that invoked `Confirm`

Task.Run(() => 2).Confirm(successCallback: v => Console.WriteLine($"Value is {v}"))
> Value is 2

Task.Run(() => {}).Confirm(successCallback: v => Console.WriteLine($"Value is {v}"),
                           errorCallback: e => Console.WriteLine(e.Message)),
                           cancelCallback: () => Console.WriteLine("This task pulled a firefly"));
> Value is 2

Home