Ported the ProgressReporter and the TrackingBar to the AsyncProgressBar control to eliminate the very similar code both controls were using to perform thread synchronization
git-svn-id: file:///srv/devel/repo-conversion/nuwi@10 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
4734c35111
commit
4261d9b449
5 changed files with 21 additions and 119 deletions
|
@ -32,7 +32,7 @@ using Nuclex.Support.Tracking;
|
|||
namespace Nuclex.Windows.Forms {
|
||||
|
||||
/// <summary>Progress bar for tracking the progress of background operations</summary>
|
||||
public partial class TrackingBar : ProgressBar {
|
||||
public partial class TrackingBar : AsyncProgressBar {
|
||||
|
||||
/// <summary>Initializes a new tracking bar</summary>
|
||||
public TrackingBar() {
|
||||
|
@ -42,21 +42,20 @@ namespace Nuclex.Windows.Forms {
|
|||
this.isIdle = true;
|
||||
base.Visible = false;
|
||||
|
||||
// Create the tracker and attach ourselfes to its events
|
||||
this.tracker = new ProgressionTracker();
|
||||
this.tracker.AsyncIdleStateChanged += this.asyncIdleStateChangedDelegate;
|
||||
this.tracker.AsyncProgressUpdated += this.asyncProgressUpdateDelegate;
|
||||
|
||||
// Initialize the delegates we use to update the control's state and those
|
||||
// we use to register ourselfes to the tracker's events
|
||||
this.updateIdleStateDelegate = new MethodInvoker(updateIdleState);
|
||||
this.updateProgressDelegate = new MethodInvoker(updateProgress);
|
||||
this.asyncIdleStateChangedDelegate = new EventHandler<IdleStateEventArgs>(
|
||||
asyncIdleStateChanged
|
||||
);
|
||||
this.asyncProgressUpdateDelegate = new EventHandler<ProgressUpdateEventArgs>(
|
||||
asyncProgressUpdated
|
||||
);
|
||||
|
||||
// Create the tracker and attach ourselfes to its events
|
||||
this.tracker = new ProgressionTracker();
|
||||
this.tracker.AsyncIdleStateChanged += this.asyncIdleStateChangedDelegate;
|
||||
this.tracker.AsyncProgressUpdated += this.asyncProgressUpdateDelegate;
|
||||
}
|
||||
|
||||
/// <summary>Tracks the specified progression in the tracking bar</summary>
|
||||
|
@ -86,22 +85,7 @@ namespace Nuclex.Windows.Forms {
|
|||
private void asyncProgressUpdated(
|
||||
object sender, ProgressUpdateEventArgs arguments
|
||||
) {
|
||||
|
||||
// Set the new progress without any synchronization
|
||||
this.currentProgress = arguments.Progress;
|
||||
|
||||
// Another use of the double-checked locking idiom, here we're trying to optimize
|
||||
// away the lock in case some "trigger-happy" progressions send way more
|
||||
// progress updates than the poor control can process :)
|
||||
if(!this.progressUpdatePending) {
|
||||
lock(this) {
|
||||
if(!this.progressUpdatePending) {
|
||||
this.progressUpdatePending = true;
|
||||
this.progressUpdateAsyncResult = BeginInvoke(this.updateProgressDelegate);
|
||||
}
|
||||
} // lock
|
||||
}
|
||||
|
||||
AsyncSetValue(arguments.Progress);
|
||||
}
|
||||
|
||||
/// <summary>Called when the tracker becomes enters of leaves the idle state</summary>
|
||||
|
@ -113,33 +97,13 @@ namespace Nuclex.Windows.Forms {
|
|||
// lost because otherwise, the progress bar might stay on-screen when in fact,
|
||||
// the background operation has already finished and nothing is happening anymore.
|
||||
this.isIdle = arguments.Idle;
|
||||
Invoke(this.updateIdleStateDelegate);
|
||||
|
||||
}
|
||||
// Update the bar's idle state
|
||||
if(InvokeRequired)
|
||||
Invoke(this.updateIdleStateDelegate);
|
||||
else
|
||||
updateIdleState();
|
||||
|
||||
/// <summary>Synchronously updates the value visualized in the progress bar</summary>
|
||||
private void updateProgress() {
|
||||
lock(this) {
|
||||
|
||||
// Reset the update flag so incoming updates will cause the control to
|
||||
// update itself another time.
|
||||
this.progressUpdatePending = false;
|
||||
EndInvoke(this.progressUpdateAsyncResult);
|
||||
|
||||
// Transform the progress into an integer in the range of the progress bar's
|
||||
// min and max values (these should normally be set to 0 and 100).
|
||||
int min = base.Minimum;
|
||||
int max = base.Maximum;
|
||||
int progress = (int)(this.currentProgress * (max - min)) + min;
|
||||
|
||||
// Update the control
|
||||
base.Value = progress;
|
||||
|
||||
// Assigning the value sends PBM_SETPOS to the control which,
|
||||
// according to MSDN, already causes a redraw!
|
||||
//base.Invalidate();
|
||||
|
||||
} // lock
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -148,23 +112,15 @@ namespace Nuclex.Windows.Forms {
|
|||
/// </summary>
|
||||
private void updateIdleState() {
|
||||
|
||||
// Only show the progress bar when something is happening
|
||||
base.Visible = !this.isIdle;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>Whether an update of the control state is pending</summary>
|
||||
private volatile bool progressUpdatePending;
|
||||
/// <summary>Async result for the invoked control state update method</summary>
|
||||
private volatile IAsyncResult progressUpdateAsyncResult;
|
||||
/// <summary>Whether the progress bar is in the idle state</summary>
|
||||
private volatile bool isIdle;
|
||||
/// <summary>Most recently reported progress of the tracker</summary>
|
||||
private volatile float currentProgress;
|
||||
|
||||
/// <summary>Tracker used to sum and update the total progress</summary>
|
||||
private ProgressionTracker tracker;
|
||||
/// <summary>Delegate for the progress update method</summary>
|
||||
private MethodInvoker updateProgressDelegate;
|
||||
/// <summary>Delegate for the idle state update method</summary>
|
||||
private MethodInvoker updateIdleStateDelegate;
|
||||
/// <summary>Delegate for the OnAsyncProgressionEnded method</summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue