using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// Progression with an associated weight for the total progress
internal class WeightedProgression : IDisposable
where ProgressionType : Progression {
/// Initializes a new weighted progression
/// Callback to pass progress updates on to
/// Progression whose progress to monitor
/// Weighting of the progression's progress
public WeightedProgression(
ProgressionType progression,
EventHandler callback,
float weight
) {
this.progression = progression;
this.weight = weight;
this.callback = callback;
progression.AsyncProgressUpdated += new EventHandler(
asyncProgressUpdated
);
}
/// Disposes of the resources used by this instance immediately
public void Dispose() {
if(this.progression != null) {
progression.AsyncProgressUpdated -= new EventHandler(
asyncProgressUpdated
);
this.progression = null;
}
}
/// Progression being wrapped by this weighted progression
public ProgressionType Progression {
get { return this.progression; }
}
/// Progress this progression has achieved so far
public float Progress {
get { return this.progress; }
}
/// The weighting of this progression in the total progress
public float Weight {
get { return this.weight; }
}
/// Handles progress reports by the progression
/// Progression that has made progress
/// Contains the currently achieved progress
private void asyncProgressUpdated(object sender, ProgressUpdateEventArgs e) {
this.progress = e.Progress;
this.callback(sender, e);
}
/// Progression whose progress we're tracking
private ProgressionType progression;
/// Callback to which any progress reports will be passed on
private EventHandler callback;
/// Most recent progress reported by the progression
private volatile float progress;
/// Weighting of this progression in the total progress
private float weight;
}
} // namespace Nuclex.Support.Tracking