busterwood / Channels

Depricated - see Goodies package

Home Page:https://github.com/busterwood/Goodies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DEPRICATED

All future development now takes place on https://github.com/busterwood/Goodies

BusterWood.Channels

Build status Nuget

Available on Nuget as BusterWood.Channels.

CSP-like channels for .NET 4.6 or above and .NET Core.

As its name suggests, CSP allows the description of systems in terms of component processes that operate independently, and interact with each other solely through message-passing communication.

Channel<T> class

The Channel<T>class is used for sending and receiving values between threads (or logical async threads) and has the following methods:

  • T Receive() reads a value from the channel, blocking until a sender has sent a value.
  • Task<T> ReceiveAsync() reads a value from the channel, the returned task will only complete when a sender has written the value to the channel.
  • bool TryReceive(out T) attempts to read a value from the channel, returns FALSE is no sender is ready.
  • void Send(T) writes a value to the channel, blocking until a receiver has got the value.
  • Task SendAsync(T) writes a value to the channel, the returned task will only complete when a receiver has got the value.
  • bool TrySend(T) attempts to write a value to the channel, returns FALSE is no receiver is ready.
  • void Close() prevents any further attempts to send to the channel
  • bool IsClosed has the channel been closed?

Select class

The Select class is used for reading from one of many possible channels, has the following methods:

  • Select OnReceive<T>(Channel<T>, Action<T>) builder method that adds an case to the select that executes a (synchronous) action when the channel is read.
  • Select OnReceiveAsync<T>(Channel<T>, Func<T, Task>) builder method that adds an case to the select that executes an asynchronous action when the channel is read.
  • Task ExecuteAsync() reads from one (and only one) of the channels and executes the associated action.
  • Task<bool> ExecuteAsync(TimeSpan) tries to read from one of the channels within the timeout, returns FALSE if no channel could be read within the timeout.

Closed channels

When Close() is called on a Channel<T> then:

  • any outstanding sends are allowed to complete
  • any new sends will throw an OperationCanceledException
  • any outstanding recevies with matching sends are allowed to complete
  • any outstanding recevies without matching sends will throw an OperationCanceledException
  • any new recevies will throw an OperationCanceledException

The bool TryReceive(out T) and bool TrySend(T) methods will always return false when a channel IsClosed.

About

Depricated - see Goodies package

https://github.com/busterwood/Goodies

License:Apache License 2.0


Languages

Language:C# 97.1%Language:PowerShell 2.9%