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
|
@ -101,6 +101,8 @@
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\ProgressReporter\ProgressReporterForm.Designer.cs">
|
<Compile Include="Source\ProgressReporter\ProgressReporterForm.Designer.cs">
|
||||||
<DependentUpon>ProgressReporterForm.cs</DependentUpon>
|
<DependentUpon>ProgressReporterForm.cs</DependentUpon>
|
||||||
|
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
||||||
|
<Name>ProgressReporterForm.Designer</Name>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\TrackingBar\TrackingBar.cs">
|
<Compile Include="Source\TrackingBar\TrackingBar.cs">
|
||||||
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
<XNAUseContentPipeline>false</XNAUseContentPipeline>
|
||||||
|
|
|
@ -73,6 +73,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
/// <summary>Synchronously updates the value visualized in the progress bar</summary>
|
/// <summary>Synchronously updates the value visualized in the progress bar</summary>
|
||||||
private void updateProgress() {
|
private void updateProgress() {
|
||||||
|
|
||||||
|
// Cache these to shorten the code that follows :)
|
||||||
int minimum = base.Minimum;
|
int minimum = base.Minimum;
|
||||||
int maximum = base.Maximum;
|
int maximum = base.Maximum;
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
private void InitializeComponent() {
|
private void InitializeComponent() {
|
||||||
this.components = new System.ComponentModel.Container();
|
this.components = new System.ComponentModel.Container();
|
||||||
this.cancelButton = new System.Windows.Forms.Button();
|
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.statusLabel = new System.Windows.Forms.Label();
|
||||||
this.controlCreationTimer = new System.Windows.Forms.Timer(this.components);
|
this.controlCreationTimer = new System.Windows.Forms.Timer(this.components);
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
|
@ -92,7 +92,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private System.Windows.Forms.Button cancelButton;
|
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.Label statusLabel;
|
||||||
private System.Windows.Forms.Timer controlCreationTimer;
|
private System.Windows.Forms.Timer controlCreationTimer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ namespace Nuclex.Windows.Forms {
|
||||||
internal ProgressReporterForm() {
|
internal ProgressReporterForm() {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
this.updateProgressDelegate = new MethodInvoker(updateProgress);
|
|
||||||
this.asyncEndedDelegate = new EventHandler(asyncEnded);
|
this.asyncEndedDelegate = new EventHandler(asyncEnded);
|
||||||
this.asyncProgressUpdatedDelegate = new EventHandler<ProgressUpdateEventArgs>(
|
this.asyncProgressUpdatedDelegate = new EventHandler<ProgressUpdateEventArgs>(
|
||||||
asyncProgressUpdated
|
asyncProgressUpdated
|
||||||
|
@ -112,8 +111,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
|
|
||||||
// If the new state is 2, the form was ready to close (since the state
|
// 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)
|
// is incremented once when the form becomes ready to be closed)
|
||||||
int newState = Interlocked.Increment(ref this.state);
|
if(Interlocked.Increment(ref this.state) == 2) {
|
||||||
if(newState == 2) {
|
|
||||||
|
|
||||||
// Close the dialog. Ensure the Close() method is invoked from the
|
// Close the dialog. Ensure the Close() method is invoked from the
|
||||||
// same thread the dialog was created in.
|
// same thread the dialog was created in.
|
||||||
|
@ -132,53 +130,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
/// Contains the new progress achieved by the progression
|
/// Contains the new progress achieved by the progression
|
||||||
/// </param>
|
/// </param>
|
||||||
private void asyncProgressUpdated(object sender, ProgressUpdateEventArgs arguments) {
|
private void asyncProgressUpdated(object sender, ProgressUpdateEventArgs arguments) {
|
||||||
|
this.progressBar.AsyncSetValue(arguments.Progress);
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
// 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!
|
// been fully constructed, so we should close it now!
|
||||||
int newState = System.Threading.Interlocked.Increment(ref this.state);
|
if(Interlocked.Increment(ref this.state) == 2)
|
||||||
if(newState == 2)
|
|
||||||
Close();
|
Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -228,14 +179,6 @@ namespace Nuclex.Windows.Forms {
|
||||||
private EventHandler asyncEndedDelegate;
|
private EventHandler asyncEndedDelegate;
|
||||||
/// <summary>Delegate for the asyncProgressUpdated() method</summary>
|
/// <summary>Delegate for the asyncProgressUpdated() method</summary>
|
||||||
private EventHandler<ProgressUpdateEventArgs> asyncProgressUpdatedDelegate;
|
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>
|
/// <summary>Whether the form can be closed and should be closed</summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// 0: Nothing happened yet
|
/// 0: Nothing happened yet
|
||||||
|
|
|
@ -32,7 +32,7 @@ using Nuclex.Support.Tracking;
|
||||||
namespace Nuclex.Windows.Forms {
|
namespace Nuclex.Windows.Forms {
|
||||||
|
|
||||||
/// <summary>Progress bar for tracking the progress of background operations</summary>
|
/// <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>
|
/// <summary>Initializes a new tracking bar</summary>
|
||||||
public TrackingBar() {
|
public TrackingBar() {
|
||||||
|
@ -42,21 +42,20 @@ namespace Nuclex.Windows.Forms {
|
||||||
this.isIdle = true;
|
this.isIdle = true;
|
||||||
base.Visible = false;
|
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
|
// Initialize the delegates we use to update the control's state and those
|
||||||
// we use to register ourselfes to the tracker's events
|
// we use to register ourselfes to the tracker's events
|
||||||
this.updateIdleStateDelegate = new MethodInvoker(updateIdleState);
|
this.updateIdleStateDelegate = new MethodInvoker(updateIdleState);
|
||||||
this.updateProgressDelegate = new MethodInvoker(updateProgress);
|
|
||||||
this.asyncIdleStateChangedDelegate = new EventHandler<IdleStateEventArgs>(
|
this.asyncIdleStateChangedDelegate = new EventHandler<IdleStateEventArgs>(
|
||||||
asyncIdleStateChanged
|
asyncIdleStateChanged
|
||||||
);
|
);
|
||||||
this.asyncProgressUpdateDelegate = new EventHandler<ProgressUpdateEventArgs>(
|
this.asyncProgressUpdateDelegate = new EventHandler<ProgressUpdateEventArgs>(
|
||||||
asyncProgressUpdated
|
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>
|
/// <summary>Tracks the specified progression in the tracking bar</summary>
|
||||||
|
@ -86,22 +85,7 @@ namespace Nuclex.Windows.Forms {
|
||||||
private void asyncProgressUpdated(
|
private void asyncProgressUpdated(
|
||||||
object sender, ProgressUpdateEventArgs arguments
|
object sender, ProgressUpdateEventArgs arguments
|
||||||
) {
|
) {
|
||||||
|
AsyncSetValue(arguments.Progress);
|
||||||
// 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>Called when the tracker becomes enters of leaves the idle state</summary>
|
/// <summary>Called when the tracker becomes enters of leaves the idle state</summary>
|
||||||
|
@ -113,58 +97,30 @@ namespace Nuclex.Windows.Forms {
|
||||||
// lost because otherwise, the progress bar might stay on-screen when in fact,
|
// lost because otherwise, the progress bar might stay on-screen when in fact,
|
||||||
// the background operation has already finished and nothing is happening anymore.
|
// the background operation has already finished and nothing is happening anymore.
|
||||||
this.isIdle = arguments.Idle;
|
this.isIdle = arguments.Idle;
|
||||||
|
|
||||||
|
// Update the bar's idle state
|
||||||
|
if(InvokeRequired)
|
||||||
Invoke(this.updateIdleStateDelegate);
|
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>
|
/// <summary>
|
||||||
/// Updates the idle state of the progress bar
|
/// Updates the idle state of the progress bar
|
||||||
/// (controls whether the progress bar is shown or invisible)
|
/// (controls whether the progress bar is shown or invisible)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void updateIdleState() {
|
private void updateIdleState() {
|
||||||
|
|
||||||
|
// Only show the progress bar when something is happening
|
||||||
base.Visible = !this.isIdle;
|
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>
|
/// <summary>Whether the progress bar is in the idle state</summary>
|
||||||
private volatile bool isIdle;
|
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>
|
/// <summary>Tracker used to sum and update the total progress</summary>
|
||||||
private ProgressionTracker tracker;
|
private ProgressionTracker tracker;
|
||||||
/// <summary>Delegate for the progress update method</summary>
|
|
||||||
private MethodInvoker updateProgressDelegate;
|
|
||||||
/// <summary>Delegate for the idle state update method</summary>
|
/// <summary>Delegate for the idle state update method</summary>
|
||||||
private MethodInvoker updateIdleStateDelegate;
|
private MethodInvoker updateIdleStateDelegate;
|
||||||
/// <summary>Delegate for the OnAsyncProgressionEnded method</summary>
|
/// <summary>Delegate for the OnAsyncProgressionEnded method</summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user