#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2007 Nuclex Development Labs
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
using System;
using System.Collections.Generic;
using Nuclex.Support.Tracking;
namespace Nuclex.Support.Scheduling {
/// Operation that sequentially executes a series of operations
///
/// Type of the child operations the QueueOperation will contain
///
public class QueueOperation : Operation
where OperationType : Operation {
/// Initializes a new queue operation
private QueueOperation() {
this.asyncOperationEndedDelegate = new EventHandler(asyncOperationEnded);
this.asyncOperationProgressUpdatedDelegate = new EventHandler(
asyncOperationProgressUpdated
);
this.children = new List>();
}
/// Initializes a new queue operation with default weights
/// Child operations to execute in this operation
///
/// All child operations will have a default weight of 1.0
///
public QueueOperation(IEnumerable childs) : this() {
// Construct a WeightedProgression with the default weight for each
// progression and wrap it in an ObservedProgression
foreach(OperationType operation in childs)
this.children.Add(new WeightedProgression(operation));
// Since all progressions have a weight of 1.0, the total weight is
// equal to the number of progressions in our list
this.totalWeight = (float)this.children.Count;
}
/// Initializes a new queue operation with custom weights
/// Child operations to execute in this operation
public QueueOperation(IEnumerable> childs) : this() {
// Construct an ObservedProgression around each of the WeightedProgressions
foreach(WeightedProgression operation in childs) {
this.children.Add(operation);
// Sum up the total weight
this.totalWeight += operation.Weight;
}
}
/// Provides access to the child operations of this queue
public IList> Children {
get { return this.children; }
}
/// Launches the background operation
public override void Begin() {
beginCurrentOperation();
}
/// Prepares the current operation and calls its Begin() method
///
/// This subscribes the queue to the events of to the current operation
/// and launches the operation by calling its Begin() method.
///
private void beginCurrentOperation() {
OperationType operation = this.children[this.currentOperationIndex].Progression;
operation.AsyncEnded += this.asyncOperationEndedDelegate;
operation.AsyncProgressUpdated += this.asyncOperationProgressUpdatedDelegate;
operation.Begin();
}
/// Disconnects from the current operation and calls its End() method
///
/// 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.
///
private void endCurrentOperation() {
OperationType operation = this.children[this.currentOperationIndex].Progression;
// Disconnect from the operation's events
operation.AsyncEnded -= this.asyncOperationEndedDelegate;
operation.AsyncProgressUpdated -= this.asyncOperationProgressUpdatedDelegate;
try {
operation.End();
// Add the operations weight to the total amount of completed weight in the queue
this.completedWeight += this.children[this.currentOperationIndex].Weight;
// Trigger another progress update
OnAsyncProgressUpdated(this.completedWeight / this.totalWeight);
}
catch(Exception exception) {
SetException(exception);
}
}
/// Called when the current executing operation ends
/// Operation that ended
/// Not used
private void asyncOperationEnded(object sender, EventArgs e) {
// Unsubscribe from the current operation's events and update the
// accumulating progress counter
endCurrentOperation();
// Only jump to the next operation if no exception occured
if(OccuredException == null) {
++this.currentOperationIndex;
// Execute the next operation unless we reached the end of the queue
if(this.currentOperationIndex < this.children.Count) {
beginCurrentOperation();
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();
}
/// Called when currently executing operation makes progress
/// Operation that has achieved progress
/// Not used
private void asyncOperationProgressUpdated(object sender, ProgressUpdateEventArgs e) {
// Determine the completed weight of the currently executing operation
float currentOperationCompletedWeight =
e.Progress * this.children[this.currentOperationIndex].Weight;
// Build the total normalized amount of progress for the queue
float progress =
(this.completedWeight + currentOperationCompletedWeight) / this.totalWeight;
// Done, we can send the actual progress to any event subscribers
OnAsyncProgressUpdated(progress);
}
/// Delegate to the asyncOperationEnded() method
private EventHandler asyncOperationEndedDelegate;
/// Delegate to the asyncOperationProgressUpdated() method
private EventHandler asyncOperationProgressUpdatedDelegate;
/// Operations being managed in the queue
private List> children;
/// Summed weight of all operations in the queue
private float totalWeight;
/// Accumulated weight of the operations already completed
private float completedWeight;
/// Index of the operation currently executing
private int currentOperationIndex;
}
} // namespace Nuclex.Support.Scheduling