Skip to content

Async Context Delegates Package

kobi2294 edited this page Apr 22, 2019 · 2 revisions

The Async Context Delegates package

This package is a subset of the Async Package that focuses on delegate wrappers that have 2 main features:

  1. They are all WeakDelegates. See Weak Package for details of weak delegates.
  2. They run in the SynchronizationContext where they were created, or on a specified TaskScheduler or ContextRunner.

Features

Constructors

All the classes in the package expose many constructors so you may pass any combinations of the followings:

  • Delegate or Weak Delegate. If you pass a "normal" delegate. A Weak delegate will be created for you, so you should also pass an owner object.
  • TaskSchduler, ContextRunner or nothing. You may pass a TaskScheduler or a ContextRunner to set the thread group that will execute the delegate. Alternatively, you may omit both of them, and the thread that runs the constructor will decide the thread group that will run the delegate.

Asyncronous Invokers

While synchronous delegates expose Invoke or Execute methods that return void or T, the context delegates schdule the delegate to run on another thread, and return instantly, so that they do not block. In order to be able to synchornize with their execution, they return Task objects.

Multicast delegates

Any delegate can turn into a multicast delegate by chainging calls, but this is problematic where each delegate must run on a different thread. Therefore, instead of chaining delegates and then wrapping the result with a ContextAction, you should use multicase context delegates, that allow you to chain context delegates. This way, each delege you chain is a weak delegate and has its own TaskSchduler to use.

  • Use Add and Remove methods to add or remove context delegates
  • Use += and -= as alternative to Add and Remove
  • Note that the list is immutable, so when you add or remove context delegates, you are actually creating a new instance of the multicast delegate.
  • Use Invoke method to run all the delegates. They will run concurrently. The method returns a Task object that complets when all invokations complete.
  • Note that the Invoke method will automatically remove "Dead" delegates (delegates whose target has been garbage collected).
  • Use the GetInvokationList method to get an array of the ContextDelegates in the list.

Classes

  • ContextAction, ContextAction<T> and ContextAction<T1, T2> - Wrap weak Action classes, that are invoked on a specific TaskScheduler. The Invoke Method returns a Task object to allow synchronization.
  • ContextFunc<TRes>, ContextFunc<T, TRes> - Wrap Weak Func classes, that are invoked on a specific TaskScheduler. The Invoke method returns Task<TRes>.
  • ContextMulticastAction, ContextMulticastAction<T>, ContextMulticastAction<T1, T2> - Wrap a collection of Action Classes, each with it's own TaskScheduler.
  • ContextMulticastFuncTask and ContextMulticastFuncTask<T> - A special case of multicast delegate, is a set of context functions that return tasks. This case was created to support async actions. Each item in the list is a ContextFuncorContextFunc<T, Task>. While they are called "Func", they are not meant to be used as a collection of functions, but rather as a collection of async actions (so an async action is actually a function that returns a Task`).
  • AsyncEvent - A special kind of ContextMulticastFuncTask the exposes async Subscribe and Unsubscribe methods.
  • AsyncEvent<T> - A special kind of AsyncEvent that has a "current value". When you subscribe to the event, it will instantly call you with the current value, and also call you again when the event is triggered. A little like BehaviorSubject in ReactiveX