2007-07-05 20:02:02 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2009-01-07 19:05:29 +00:00
|
|
|
Copyright (C) 2002-2009 Nuclex Development Labs
|
2007-07-05 20:02:02 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
License along with this library
|
|
|
|
*/
|
|
|
|
#endregion
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using Nuclex.Support.Tracking;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Scheduling {
|
|
|
|
|
|
|
|
/// <summary>Operation that sequentially executes a series of operations</summary>
|
|
|
|
/// <typeparam name="OperationType">
|
|
|
|
/// Type of the child operations the QueueOperation will contain
|
|
|
|
/// </typeparam>
|
2008-06-11 20:28:08 +00:00
|
|
|
public class OperationQueue<OperationType> : Operation, IProgressReporter
|
2007-07-05 20:02:02 +00:00
|
|
|
where OperationType : Operation {
|
|
|
|
|
2008-03-26 21:03:49 +00:00
|
|
|
/// <summary>will be triggered to report when progress has been achieved</summary>
|
2008-03-27 18:45:09 +00:00
|
|
|
public event EventHandler<ProgressReportEventArgs> AsyncProgressChanged;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
/// <summary>Initializes a new queue operation with default weights</summary>
|
2007-07-05 20:02:02 +00:00
|
|
|
/// <param name="childs">Child operations to execute in this operation</param>
|
|
|
|
/// <remarks>
|
|
|
|
/// All child operations will have a default weight of 1.0
|
|
|
|
/// </remarks>
|
2008-06-11 20:28:08 +00:00
|
|
|
public OperationQueue(IEnumerable<OperationType> childs) : this() {
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
// Construct a WeightedTransaction with the default weight for each
|
|
|
|
// transaction and wrap it in an ObservedTransaction
|
2007-07-10 18:15:34 +00:00
|
|
|
foreach(OperationType operation in childs)
|
2008-12-03 18:58:20 +00:00
|
|
|
this.children.Add(new WeightedTransaction<OperationType>(operation));
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
// Since all transactions have a weight of 1.0, the total weight is
|
|
|
|
// equal to the number of transactions in our list
|
2007-07-10 19:25:18 +00:00
|
|
|
this.totalWeight = (float)this.children.Count;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Initializes a new queue operation with custom weights</summary>
|
|
|
|
/// <param name="childs">Child operations to execute in this operation</param>
|
2008-12-03 18:58:20 +00:00
|
|
|
public OperationQueue(IEnumerable<WeightedTransaction<OperationType>> childs) : this() {
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
// Construct an ObservedTransactionn around each of the WeightedTransactions
|
|
|
|
foreach(WeightedTransaction<OperationType> operation in childs) {
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children.Add(operation);
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Sum up the total weight
|
|
|
|
this.totalWeight += operation.Weight;
|
|
|
|
}
|
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
}
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2008-03-26 21:03:49 +00:00
|
|
|
/// <summary>Initializes a new queue operation</summary>
|
2008-06-11 20:28:08 +00:00
|
|
|
private OperationQueue() {
|
2008-03-26 21:03:49 +00:00
|
|
|
this.asyncOperationEndedDelegate = new EventHandler(asyncOperationEnded);
|
|
|
|
this.asyncOperationProgressChangedDelegate = new EventHandler<ProgressReportEventArgs>(
|
|
|
|
asyncOperationProgressChanged
|
|
|
|
);
|
|
|
|
|
2008-12-03 18:58:20 +00:00
|
|
|
this.children = new List<WeightedTransaction<OperationType>>();
|
2008-03-26 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Provides access to the child operations of this queue</summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
public IList<WeightedTransaction<OperationType>> Children {
|
2007-07-10 19:25:18 +00:00
|
|
|
get { return this.children; }
|
2007-07-10 18:15:34 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
/// <summary>Launches the background operation</summary>
|
2008-02-07 21:25:34 +00:00
|
|
|
public override void Start() {
|
|
|
|
startCurrentOperation();
|
2007-07-10 18:15:34 +00:00
|
|
|
}
|
2007-07-05 20:02:02 +00:00
|
|
|
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Allows the specific request implementation to re-throw an exception if
|
|
|
|
/// the background process finished unsuccessfully
|
|
|
|
/// </summary>
|
|
|
|
protected override void ReraiseExceptions() {
|
|
|
|
if(this.exception != null)
|
|
|
|
throw this.exception;
|
|
|
|
}
|
|
|
|
|
2008-03-26 21:03:49 +00:00
|
|
|
/// <summary>Fires the progress update event</summary>
|
|
|
|
/// <param name="progress">Progress to report (ranging from 0.0 to 1.0)</param>
|
|
|
|
/// <remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Informs the observers of this transaction about the achieved progress.
|
2008-03-26 21:03:49 +00:00
|
|
|
/// </remarks>
|
|
|
|
protected virtual void OnAsyncProgressChanged(float progress) {
|
|
|
|
OnAsyncProgressChanged(new ProgressReportEventArgs(progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Fires the progress update event</summary>
|
|
|
|
/// <param name="eventArguments">Progress to report (ranging from 0.0 to 1.0)</param>
|
|
|
|
/// <remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Informs the observers of this transaction about the achieved progress.
|
|
|
|
/// Allows for classes derived from the transaction class to easily provide
|
2008-03-26 21:03:49 +00:00
|
|
|
/// a custom event arguments class that has been derived from the
|
2008-12-03 18:58:20 +00:00
|
|
|
/// transaction's ProgressUpdateEventArgs class.
|
2008-03-26 21:03:49 +00:00
|
|
|
/// </remarks>
|
|
|
|
protected virtual void OnAsyncProgressChanged(ProgressReportEventArgs eventArguments) {
|
2008-03-27 18:45:09 +00:00
|
|
|
EventHandler<ProgressReportEventArgs> copy = AsyncProgressChanged;
|
2008-03-26 21:03:49 +00:00
|
|
|
if(copy != null)
|
|
|
|
copy(this, eventArguments);
|
|
|
|
}
|
|
|
|
|
2009-05-05 19:31:05 +00:00
|
|
|
/// <summary>Prepares the current operation and calls its Start() method</summary>
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// This subscribes the queue to the events of to the current operation
|
2009-05-05 19:31:05 +00:00
|
|
|
/// and launches the operation by calling its Start() method.
|
2007-07-10 18:15:34 +00:00
|
|
|
/// </remarks>
|
2008-02-07 21:25:34 +00:00
|
|
|
private void startCurrentOperation() {
|
2008-12-03 18:58:20 +00:00
|
|
|
OperationType operation = this.children[this.currentOperationIndex].Transaction;
|
2007-07-05 20:02:02 +00:00
|
|
|
|
2007-07-10 18:15:34 +00:00
|
|
|
operation.AsyncEnded += this.asyncOperationEndedDelegate;
|
2008-03-26 21:03:49 +00:00
|
|
|
|
|
|
|
IProgressReporter progressReporter = operation as IProgressReporter;
|
|
|
|
if(progressReporter != null)
|
|
|
|
progressReporter.AsyncProgressChanged += this.asyncOperationProgressChangedDelegate;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2008-02-07 21:25:34 +00:00
|
|
|
operation.Start();
|
2007-07-05 20:02:02 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Disconnects from the current operation and calls its End() method</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This unsubscribes the queue from the current operation's events, calls End()
|
|
|
|
/// on the operation and, if the operation didn't have an exception to report,
|
|
|
|
/// counts up the accumulated progress of the queue.
|
|
|
|
/// </remarks>
|
|
|
|
private void endCurrentOperation() {
|
2008-12-03 18:58:20 +00:00
|
|
|
OperationType operation = this.children[this.currentOperationIndex].Transaction;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Disconnect from the operation's events
|
|
|
|
operation.AsyncEnded -= this.asyncOperationEndedDelegate;
|
2008-03-26 21:03:49 +00:00
|
|
|
|
|
|
|
IProgressReporter progressReporter = operation as IProgressReporter;
|
|
|
|
if(progressReporter != null)
|
|
|
|
progressReporter.AsyncProgressChanged -= this.asyncOperationProgressChangedDelegate;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
try {
|
2008-02-07 21:25:34 +00:00
|
|
|
operation.Join();
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Add the operations weight to the total amount of completed weight in the queue
|
2007-07-10 19:25:18 +00:00
|
|
|
this.completedWeight += this.children[this.currentOperationIndex].Weight;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Trigger another progress update
|
2008-03-26 21:03:49 +00:00
|
|
|
OnAsyncProgressChanged(this.completedWeight / this.totalWeight);
|
2007-07-10 18:15:34 +00:00
|
|
|
}
|
|
|
|
catch(Exception exception) {
|
2008-03-26 20:20:38 +00:00
|
|
|
this.exception = exception;
|
2007-07-10 18:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Called when the current executing operation ends</summary>
|
|
|
|
/// <param name="sender">Operation that ended</param>
|
2009-05-05 19:31:05 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
private void asyncOperationEnded(object sender, EventArgs arguments) {
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Unsubscribe from the current operation's events and update the
|
|
|
|
// accumulating progress counter
|
|
|
|
endCurrentOperation();
|
|
|
|
|
|
|
|
// Only jump to the next operation if no exception occured
|
2008-03-26 20:20:38 +00:00
|
|
|
if(this.exception == null) {
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
++this.currentOperationIndex;
|
|
|
|
|
|
|
|
// Execute the next operation unless we reached the end of the queue
|
2007-07-10 19:25:18 +00:00
|
|
|
if(this.currentOperationIndex < this.children.Count) {
|
2008-02-07 21:25:34 +00:00
|
|
|
startCurrentOperation();
|
2007-07-10 18:15:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either an exception has occured or we reached the end of the operation
|
|
|
|
// queue. In any case, we need to report that the operation is over.
|
|
|
|
OnAsyncEnded();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Called when currently executing operation makes progress</summary>
|
|
|
|
/// <param name="sender">Operation that has achieved progress</param>
|
2009-05-05 19:31:05 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
private void asyncOperationProgressChanged(
|
|
|
|
object sender, ProgressReportEventArgs arguments
|
|
|
|
) {
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Determine the completed weight of the currently executing operation
|
2009-05-05 19:31:05 +00:00
|
|
|
float operationWeight = this.children[this.currentOperationIndex].Weight;
|
|
|
|
float operationCompletedWeight = arguments.Progress * operationWeight;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Build the total normalized amount of progress for the queue
|
2009-05-05 19:31:05 +00:00
|
|
|
float progress = (this.completedWeight + operationCompletedWeight) / this.totalWeight;
|
2007-07-10 18:15:34 +00:00
|
|
|
|
|
|
|
// Done, we can send the actual progress to any event subscribers
|
2008-03-26 21:03:49 +00:00
|
|
|
OnAsyncProgressChanged(progress);
|
2007-07-10 18:15:34 +00:00
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Delegate to the asyncOperationEnded() method</summary>
|
|
|
|
private EventHandler asyncOperationEndedDelegate;
|
|
|
|
/// <summary>Delegate to the asyncOperationProgressUpdated() method</summary>
|
2008-03-26 21:03:49 +00:00
|
|
|
private EventHandler<ProgressReportEventArgs> asyncOperationProgressChangedDelegate;
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Operations being managed in the queue</summary>
|
2008-12-03 18:58:20 +00:00
|
|
|
private List<WeightedTransaction<OperationType>> children;
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Summed weight of all operations in the queue</summary>
|
|
|
|
private float totalWeight;
|
|
|
|
/// <summary>Accumulated weight of the operations already completed</summary>
|
|
|
|
private float completedWeight;
|
|
|
|
/// <summary>Index of the operation currently executing</summary>
|
|
|
|
private int currentOperationIndex;
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>Exception that has occured in the background process</summary>
|
|
|
|
private volatile Exception exception;
|
2007-07-05 20:02:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Scheduling
|