//******************************************************************************************************
// AsyncDoubleBufferedQueue.cs - Gbtc
//
// Copyright © 2013, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 06/14/2013 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
namespace GSF.Collections
{
///
/// Combines and to provide
/// a low-contention, double-buffered queue suitable for multiple-producer, single-consumer
/// scenarios.
///
/// Type of items being queued.
public class AsyncDoubleBufferedQueue
{
#region [ Members ]
// Events
///
/// Event that is raised if an exception is encountered while attempting to processing an item in the .
///
///
/// Processing will not stop for any exceptions thrown by user processing function, but exceptions will be exposed through this event.
///
public event EventHandler> ProcessException;
// Fields
private AsyncQueue> m_asyncQueue;
private DoubleBufferedQueue m_doubleBufferedQueue;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
public AsyncDoubleBufferedQueue()
{
m_asyncQueue = new AsyncQueue>();
m_doubleBufferedQueue = new DoubleBufferedQueue();
m_asyncQueue.ProcessItemFunction = item => m_doubleBufferedQueue.Enqueue(item);
m_asyncQueue.ProcessException += (sender, args) => OnProcessException(args.Argument);
m_doubleBufferedQueue.ProcessException += (sender, args) => OnProcessException(args.Argument);
}
#endregion
#region [ Properties ]
///
/// Gets or sets item processing function.
///
public Action> ProcessItemsFunction
{
get
{
return m_doubleBufferedQueue.ProcessItemsFunction;
}
set
{
m_doubleBufferedQueue.ProcessItemsFunction = value;
}
}
///
/// Gets the number of items in the queue.
///
public int Count
{
get
{
return m_asyncQueue.Count + m_doubleBufferedQueue.Count;
}
}
#endregion
#region [ Methods ]
///
/// Enqueues a collection of items into the async double-buffered queue.
///
/// The items to be queued.
public void Enqueue(IEnumerable items)
{
m_asyncQueue.Enqueue(items);
}
// Notifies the consumer of exceptions that occur while processing items in the AsyncQueue.
private void OnProcessException(Exception ex)
{
if ((object)ProcessException != null)
ProcessException(this, new EventArgs(ex));
}
#endregion
}
}