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:
Markus Ewald 2007-07-22 21:55:35 +00:00
parent 4734c35111
commit 4261d9b449
5 changed files with 21 additions and 119 deletions

View File

@ -101,6 +101,8 @@
</Compile>
<Compile Include="Source\ProgressReporter\ProgressReporterForm.Designer.cs">
<DependentUpon>ProgressReporterForm.cs</DependentUpon>
<XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>ProgressReporterForm.Designer</Name>
</Compile>
<Compile Include="Source\TrackingBar\TrackingBar.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline>

View File

@ -73,6 +73,7 @@ namespace Nuclex.Windows.Forms {
/// <summary>Synchronously updates the value visualized in the progress bar</summary>
private void updateProgress() {
// Cache these to shorten the code that follows :)
int minimum = base.Minimum;
int maximum = base.Maximum;

View File

@ -25,7 +25,7 @@ namespace Nuclex.Windows.Forms {
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.cancelButton = new System.Windows.Forms.Button();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.progressBar = new Nuclex.Windows.Forms.AsyncProgressBar();
this.statusLabel = new System.Windows.Forms.Label();
this.controlCreationTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
@ -92,7 +92,7 @@ namespace Nuclex.Windows.Forms {
#endregion
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.ProgressBar progressBar;
private Nuclex.Windows.Forms.AsyncProgressBar progressBar;
private System.Windows.Forms.Label statusLabel;
private System.Windows.Forms.Timer controlCreationTimer;
}

View File

@ -22,7 +22,6 @@ namespace Nuclex.Windows.Forms {
internal ProgressReporterForm() {
InitializeComponent();
this.updateProgressDelegate = new MethodInvoker(updateProgress);
this.asyncEndedDelegate = new EventHandler(asyncEnded);
this.asyncProgressUpdatedDelegate = new EventHandler<ProgressUpdateEventArgs>(
asyncProgressUpdated
@ -112,8 +111,7 @@ namespace Nuclex.Windows.Forms {
// If the new state is 2, the form was ready to close (since the state
// is incremented once when the form becomes ready to be closed)
int newState = Interlocked.Increment(ref this.state);
if(newState == 2) {
if(Interlocked.Increment(ref this.state) == 2) {
// Close the dialog. Ensure the Close() method is invoked from the
// same thread the dialog was created in.
@ -132,53 +130,7 @@ namespace Nuclex.Windows.Forms {
/// Contains the new progress achieved by the progression
/// </param>
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
}
}
/// <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);
// Until the first progress event is received, the progress reporter shows
// a marquee bar to entertain the user even when no progress reports are
// being made at all.
if(this.progressBar.Style == ProgressBarStyle.Marquee)
this.progressBar.Style = ProgressBarStyle.Blocks;
// 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 = this.progressBar.Minimum;
int max = this.progressBar.Maximum;
int progress = (int)(this.currentProgress * (max - min)) + min;
// Update the control
this.progressBar.Value = Math.Min(Math.Max(progress, min), max);
// Assigning the value sends PBM_SETPOS to the control which,
// according to MSDN, already causes a redraw!
//base.Invalidate();
} // lock
this.progressBar.AsyncSetValue(arguments.Progress);
}
/// <summary>
@ -196,8 +148,7 @@ namespace Nuclex.Windows.Forms {
// If the new state is 2, then the form was requested to close before it had
// been fully constructed, so we should close it now!
int newState = System.Threading.Interlocked.Increment(ref this.state);
if(newState == 2)
if(Interlocked.Increment(ref this.state) == 2)
Close();
}
@ -228,14 +179,6 @@ namespace Nuclex.Windows.Forms {
private EventHandler asyncEndedDelegate;
/// <summary>Delegate for the asyncProgressUpdated() method</summary>
private EventHandler<ProgressUpdateEventArgs> asyncProgressUpdatedDelegate;
/// <summary>Delegate for the progress update method</summary>
private MethodInvoker updateProgressDelegate;
/// <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>Most recently reported progress of the tracker</summary>
private volatile float currentProgress;
/// <summary>Whether the form can be closed and should be closed</summary>
/// <remarks>
/// 0: Nothing happened yet

View File

@ -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>