[英]How can I gain access input queue of ActionBlock?
我正在傳遞給某個類的 Actionblock 實例。 如果我打電話
cancellationSource.Cancel();
然后處理將停止。 但是有些實例可以留在 ActionBlock 的輸入隊列中。 我需要訪問組織中剩余的實例以釋放一些資源。
我怎樣才能實現這個目標?
如果您迫切需要一個帶有公開輸入緩沖區的ActionBlock
,您可以嘗試下面的自定義實現。 它支持ActionBlock
所有內置功能,還包括自定義IEnumerable<T> InputQueue
屬性。 當ActionBlockEx
在錯誤或取消狀態下完成時,不會清空輸入緩沖區。
public class ActionBlockEx<T> : ITargetBlock<T>
{
private readonly ITargetBlock<object> _actionBlock;
private readonly Queue<T> _queue;
public ActionBlockEx(Func<T, Task> action,
ExecutionDataflowBlockOptions dataflowBlockOptions = null)
{
if (action == null) throw new ArgumentNullException(nameof(action));
_actionBlock = new ActionBlock<object>(_ =>
{
T item; lock (_queue) item = _queue.Dequeue();
return action(item);
}, dataflowBlockOptions ?? new ExecutionDataflowBlockOptions());
_queue = new Queue<T>();
}
public ActionBlockEx(Action<T> action,
ExecutionDataflowBlockOptions dataflowBlockOptions = null) : this(
item => { action(item); return Task.CompletedTask; }, dataflowBlockOptions)
{
if (action == null) throw new ArgumentNullException(nameof(action));
}
public int InputCount { get { lock (_queue) return _queue.Count; } }
public IEnumerable<T> InputQueue { get { lock (_queue) return _queue.ToList(); } }
public Task Completion => _actionBlock.Completion;
public void Complete() => _actionBlock.Complete();
void IDataflowBlock.Fault(Exception ex) => _actionBlock.Fault(ex);
DataflowMessageStatus ITargetBlock<T>.OfferMessage(DataflowMessageHeader header,
T item, ISourceBlock<T> source, bool consumeToAccept)
{
var sourceProxy = source != null ? new SourceProxy(source, this) : null;
lock (_queue)
{
var offerResult = _actionBlock.OfferMessage(header, null, sourceProxy,
consumeToAccept);
if (offerResult == DataflowMessageStatus.Accepted
&& (sourceProxy == null || !sourceProxy.ConsumeMessageInvoked))
{
_queue.Enqueue(item);
}
return offerResult;
}
}
private class SourceProxy : ISourceBlock<object>
{
private readonly ISourceBlock<T> _realSource;
private readonly ActionBlockEx<T> _realTarget;
public bool ConsumeMessageInvoked { get; private set; }
public SourceProxy(ISourceBlock<T> realSource, ActionBlockEx<T> realTarget)
{
_realSource = realSource;
_realTarget = realTarget;
}
object ISourceBlock<object>.ConsumeMessage(DataflowMessageHeader header,
ITargetBlock<object> target, out bool messageConsumed)
{
this.ConsumeMessageInvoked = true;
lock (_realTarget._queue)
{
var item = _realSource.ConsumeMessage(header, _realTarget,
out messageConsumed);
if (messageConsumed) _realTarget._queue.Enqueue(item);
}
return null;
}
bool ISourceBlock<object>.ReserveMessage(DataflowMessageHeader header,
ITargetBlock<object> target)
{
return _realSource.ReserveMessage(header, _realTarget);
}
void ISourceBlock<object>.ReleaseReservation(DataflowMessageHeader header,
ITargetBlock<object> target)
{
_realSource.ReleaseReservation(header, _realTarget);
}
Task IDataflowBlock.Completion => throw new NotSupportedException();
void IDataflowBlock.Complete() => throw new NotSupportedException();
void IDataflowBlock.Fault(Exception ex) => throw new NotSupportedException();
IDisposable ISourceBlock<object>.LinkTo(ITargetBlock<object> target,
DataflowLinkOptions linkOptions) => throw new NotSupportedException();
}
}
此實現基於一個內部ActionBlock<object>
,它提供了虛擬null
消息。 它與鏈接的ISourceBlock
通信被攔截,以便獲取實際消息並將其存儲在內部Queue<T>
。 這種間接性增加了一些開銷(在收到的每條消息上都會進行對象分配),因此請謹慎使用此類!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.