#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
#if WITH_NUCLEX_SUPPORT_TRANSACTIONS
using Nuclex.Support.Tracking;
namespace Nuclex.Windows.Forms {
/// Progress bar for tracking the progress of background operations
public partial class TrackingBar : AsyncProgressBar {
/// Initializes a new tracking bar
public TrackingBar() {
InitializeComponent();
// We start off being in the idle state (and thus, being invisible)
this.isIdle = true;
this.Visible = false;
// 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(
asyncIdleStateChanged
);
this.asyncProgressUpdateDelegate = new EventHandler(
asyncProgressUpdated
);
// Create the tracker and attach ourselfes to its events
this.tracker = new ProgressTracker();
this.tracker.AsyncIdleStateChanged += this.asyncIdleStateChangedDelegate;
this.tracker.AsyncProgressChanged += this.asyncProgressUpdateDelegate;
}
/// Tracks the specified transaction in the tracking bar
/// Transaction to be tracked
public void Track(Transaction transaction) {
this.tracker.Track(transaction);
}
/// Tracks the specified transaction in the tracking bar
/// Transaction to be tracked
/// Weight of this transaction in the total progress
public void Track(Transaction transaction, float weight) {
this.tracker.Track(transaction, weight);
}
/// Stops tracking the specified transaction
/// Transaction to stop tracking
public void Untrack(Transaction transaction) {
this.tracker.Untrack(transaction);
}
///
/// Called when the summed progressed of the tracked transaction has changed
///
/// Transaction whose progress has changed
/// Contains the progress achieved by the transaction
private void asyncProgressUpdated(
object sender, ProgressReportEventArgs arguments
) {
AsyncSetValue(arguments.Progress);
}
/// Called when the tracker becomes enters of leaves the idle state
/// Tracker that has entered or left the idle state
/// Contains the new idle state
private void asyncIdleStateChanged(object sender, IdleStateEventArgs arguments) {
// 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;
// Update the bar's idle state
if(InvokeRequired) {
Invoke(this.updateIdleStateDelegate);
} else {
updateIdleState();
}
}
///
/// Updates the idle state of the progress bar
/// (controls whether the progress bar is shown or invisible)
///
private void updateIdleState() {
// Only show the progress bar when something is happening
base.Visible = !this.isIdle;
}
/// Whether the progress bar is in the idle state
private volatile bool isIdle;
/// Tracker used to sum and update the total progress
private ProgressTracker tracker;
/// Delegate for the idle state update method
private MethodInvoker updateIdleStateDelegate;
/// Delegate for the asyncIdleStateChanged() method
private EventHandler asyncIdleStateChangedDelegate;
/// Delegate for the asyncProgressUpdate() method
private EventHandler asyncProgressUpdateDelegate;
}
} // namespace Nuclex.Windows.Forms
#endif // WITH_NUCLEX_SUPPORT_TRANSACTIONS