2007-07-16 19:00:02 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2019-02-01 20:06:36 +00:00
|
|
|
Copyright (C) 2002-2019 Nuclex Development Labs
|
2007-07-16 19:00:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-05-11 21:23:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Data;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
2007-07-16 19:00:02 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
using Nuclex.Support.Tracking;
|
2007-05-11 21:23:35 +00:00
|
|
|
|
|
|
|
namespace Nuclex.Windows.Forms {
|
|
|
|
|
|
|
|
/// <summary>Progress bar for tracking the progress of background operations</summary>
|
2007-07-22 21:55:35 +00:00
|
|
|
public partial class TrackingBar : AsyncProgressBar {
|
2007-05-11 21:23:35 +00:00
|
|
|
|
|
|
|
/// <summary>Initializes a new tracking bar</summary>
|
|
|
|
public TrackingBar() {
|
|
|
|
InitializeComponent();
|
2007-07-16 19:00:02 +00:00
|
|
|
|
|
|
|
// We start off being in the idle state (and thus, being invisible)
|
|
|
|
this.isIdle = true;
|
2007-12-04 20:54:42 +00:00
|
|
|
this.Visible = false;
|
2007-07-22 21:55:35 +00:00
|
|
|
|
2007-07-16 19:00:02 +00:00
|
|
|
// 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.asyncIdleStateChangedDelegate = new EventHandler<IdleStateEventArgs>(
|
|
|
|
asyncIdleStateChanged
|
|
|
|
);
|
2008-03-27 18:34:17 +00:00
|
|
|
this.asyncProgressUpdateDelegate = new EventHandler<ProgressReportEventArgs>(
|
2007-07-16 19:00:02 +00:00
|
|
|
asyncProgressUpdated
|
|
|
|
);
|
2007-12-04 20:54:42 +00:00
|
|
|
|
|
|
|
// Create the tracker and attach ourselfes to its events
|
2008-03-27 18:34:17 +00:00
|
|
|
this.tracker = new ProgressTracker();
|
2007-12-04 20:54:42 +00:00
|
|
|
this.tracker.AsyncIdleStateChanged += this.asyncIdleStateChangedDelegate;
|
2008-03-27 18:34:17 +00:00
|
|
|
this.tracker.AsyncProgressChanged += this.asyncProgressUpdateDelegate;
|
2007-07-16 19:00:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Tracks the specified transaction in the tracking bar</summary>
|
|
|
|
/// <param name="transaction">Transaction to be tracked</param>
|
|
|
|
public void Track(Transaction transaction) {
|
|
|
|
this.tracker.Track(transaction);
|
2007-07-16 19:00:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Tracks the specified transaction in the tracking bar</summary>
|
|
|
|
/// <param name="transaction">Transaction to be tracked</param>
|
|
|
|
/// <param name="weight">Weight of this transaction in the total progress</param>
|
|
|
|
public void Track(Transaction transaction, float weight) {
|
|
|
|
this.tracker.Track(transaction, weight);
|
2007-07-16 19:00:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Stops tracking the specified transaction</summary>
|
|
|
|
/// <param name="transaction">Transaction to stop tracking</param>
|
|
|
|
public void Untrack(Transaction transaction) {
|
|
|
|
this.tracker.Untrack(transaction);
|
2007-07-16 19:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// Called when the summed progressed of the tracked transaction has changed
|
2007-07-16 19:00:02 +00:00
|
|
|
/// </summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="sender">Transaction whose progress has changed</param>
|
|
|
|
/// <param name="arguments">Contains the progress achieved by the transaction</param>
|
2007-07-16 19:00:02 +00:00
|
|
|
private void asyncProgressUpdated(
|
2008-03-27 18:34:17 +00:00
|
|
|
object sender, ProgressReportEventArgs arguments
|
2007-07-16 19:00:02 +00:00
|
|
|
) {
|
2007-07-22 21:55:35 +00:00
|
|
|
AsyncSetValue(arguments.Progress);
|
2007-05-11 21:23:35 +00:00
|
|
|
}
|
2008-12-14 18:06:31 +00:00
|
|
|
|
2007-07-16 19:00:02 +00:00
|
|
|
/// <summary>Called when the tracker becomes enters of leaves the idle state</summary>
|
|
|
|
/// <param name="sender">Tracker that has entered or left the idle state</param>
|
|
|
|
/// <param name="arguments">Contains the new idle state</param>
|
|
|
|
private void asyncIdleStateChanged(object sender, IdleStateEventArgs arguments) {
|
2008-12-14 18:06:31 +00:00
|
|
|
|
2007-07-16 19:00:02 +00:00
|
|
|
// Do a fully synchronous update of the idle state. This update must not be
|
|
|
|
// 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;
|
|
|
|
|
2007-07-22 21:55:35 +00:00
|
|
|
// Update the bar's idle state
|
2008-12-14 18:06:31 +00:00
|
|
|
if(InvokeRequired) {
|
2007-07-22 21:55:35 +00:00
|
|
|
Invoke(this.updateIdleStateDelegate);
|
2008-12-14 18:06:31 +00:00
|
|
|
} else {
|
2007-07-22 21:55:35 +00:00
|
|
|
updateIdleState();
|
2008-12-14 18:06:31 +00:00
|
|
|
}
|
2007-07-16 19:00:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Updates the idle state of the progress bar
|
|
|
|
/// (controls whether the progress bar is shown or invisible)
|
|
|
|
/// </summary>
|
|
|
|
private void updateIdleState() {
|
|
|
|
|
2007-07-22 21:55:35 +00:00
|
|
|
// Only show the progress bar when something is happening
|
2007-07-16 19:00:02 +00:00
|
|
|
base.Visible = !this.isIdle;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Whether the progress bar is in the idle state</summary>
|
|
|
|
private volatile bool isIdle;
|
|
|
|
/// <summary>Tracker used to sum and update the total progress</summary>
|
2008-03-27 18:34:17 +00:00
|
|
|
private ProgressTracker tracker;
|
2007-07-16 19:00:02 +00:00
|
|
|
/// <summary>Delegate for the idle state update method</summary>
|
|
|
|
private MethodInvoker updateIdleStateDelegate;
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Delegate for the asyncIdleStateChanged() method</summary>
|
2007-07-16 19:00:02 +00:00
|
|
|
private EventHandler<IdleStateEventArgs> asyncIdleStateChangedDelegate;
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Delegate for the asyncProgressUpdate() method</summary>
|
2008-03-27 18:34:17 +00:00
|
|
|
private EventHandler<ProgressReportEventArgs> asyncProgressUpdateDelegate;
|
2007-05-11 21:23:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Windows.Forms
|