2019-02-01 20:06:36 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
|
|
|
Copyright (C) 2002-2019 Nuclex Development Labs
|
|
|
|
|
|
|
|
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-07-17 19:33:18 +00:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Threading;
|
2019-02-01 20:06:36 +00:00
|
|
|
using System.Windows.Forms;
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
using Nuclex.Support.Scheduling;
|
2008-08-14 21:19:02 +00:00
|
|
|
using Nuclex.Support.Tracking;
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
namespace Nuclex.Windows.Forms {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Blocking progress dialog that prevents the user from accessing the application
|
2008-10-29 20:50:55 +00:00
|
|
|
/// window during a modal asynchronous processes.
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </summary>
|
2007-09-12 18:52:41 +00:00
|
|
|
/// <example>
|
2009-11-03 19:41:14 +00:00
|
|
|
/// <code>
|
|
|
|
/// class Test : Nuclex.Support.Scheduling.ThreadOperation {
|
|
|
|
///
|
|
|
|
/// static void Main() {
|
|
|
|
/// Test myTest = new Test();
|
|
|
|
/// myTest.Begin();
|
|
|
|
/// Nuclex.Windows.Forms.ProgressReporterForm.Track(myTest);
|
|
|
|
/// myTest.End();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// protected override void Execute() {
|
|
|
|
/// for(int i = 0; i < 10000000; ++i) {
|
|
|
|
/// OnAsyncProgressUpdated((float)i / 10000000.0f);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2007-09-12 18:52:41 +00:00
|
|
|
/// }
|
2009-11-03 19:41:14 +00:00
|
|
|
/// </code>
|
2007-09-12 18:52:41 +00:00
|
|
|
/// </example>
|
2007-07-17 19:33:18 +00:00
|
|
|
public partial class ProgressReporterForm : Form {
|
|
|
|
|
|
|
|
/// <summary>Initializes a new progress reporter</summary>
|
|
|
|
internal ProgressReporterForm() {
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
this.asyncEndedDelegate = new EventHandler(asyncEnded);
|
2008-03-27 18:34:17 +00:00
|
|
|
this.asyncProgressChangedDelegate = new EventHandler<ProgressReportEventArgs>(
|
|
|
|
asyncProgressChanged
|
2007-07-17 19:33:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// Shows the progress reporter until the specified transaction has ended.
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="transaction">
|
|
|
|
/// Transaction for whose duration to show the progress reporter
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </param>
|
2008-12-14 18:06:31 +00:00
|
|
|
public static void Track(Transaction transaction) {
|
|
|
|
Track(null, transaction);
|
2007-07-17 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// Shows the progress reporter until the specified transaction has ended.
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="windowTitle">
|
|
|
|
/// Text to be shown in the progress reporter's title bar
|
|
|
|
/// </param>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="transaction">
|
2008-10-29 20:50:55 +00:00
|
|
|
/// Process for whose duration to show the progress reporter
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </param>
|
2008-12-14 18:06:31 +00:00
|
|
|
public static void Track(string windowTitle, Transaction transaction) {
|
2007-07-17 19:33:18 +00:00
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
// Small optimization to avoid the lengthy control creation when the background
|
2008-10-29 20:50:55 +00:00
|
|
|
// process has already ended. This is an accepted race condition: If the process
|
|
|
|
// finishes right after this line, it doesn't change the outcome, it just
|
|
|
|
// causes the progress dialog to be constructed needlessly.
|
2019-02-01 20:06:36 +00:00
|
|
|
if(transaction.Ended) {
|
2007-07-17 19:33:18 +00:00
|
|
|
return;
|
2019-02-01 20:06:36 +00:00
|
|
|
}
|
2007-07-17 19:33:18 +00:00
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
// Open the form and let it monitor the transaction's state
|
2007-07-17 19:33:18 +00:00
|
|
|
using(ProgressReporterForm theForm = new ProgressReporterForm()) {
|
2008-12-14 18:06:31 +00:00
|
|
|
theForm.track(windowTitle, transaction);
|
2007-07-17 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Called when the user tries to close the form manually</summary>
|
2009-04-01 19:38:32 +00:00
|
|
|
/// <param name="arguments">
|
|
|
|
/// Contains a flag that can be used to abort the close attempt
|
|
|
|
/// </param>
|
|
|
|
protected override void OnClosing(CancelEventArgs arguments) {
|
|
|
|
base.OnClosing(arguments);
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
// Only allow the form to close when the form is ready to close and the
|
2008-12-14 18:06:31 +00:00
|
|
|
// transaction being tracked has also finished.
|
2009-04-01 19:38:32 +00:00
|
|
|
arguments.Cancel = (Thread.VolatileRead(ref this.state) < 2);
|
2007-07-17 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// Shows the progress reporter until the specified transaction has ended.
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="windowTitle">
|
|
|
|
/// Text to be shown in the progress reporter's title bar
|
|
|
|
/// </param>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="transaction">
|
|
|
|
/// Transaction for whose duration to show the progress reporter
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </param>
|
2008-12-14 18:06:31 +00:00
|
|
|
private void track(string windowTitle, Transaction transaction) {
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
// Set the window title if the user wants to use a custom one
|
2019-02-01 20:06:36 +00:00
|
|
|
if(windowTitle != null) {
|
2007-07-17 19:33:18 +00:00
|
|
|
Text = windowTitle;
|
2019-02-01 20:06:36 +00:00
|
|
|
}
|
2007-07-17 19:33:18 +00:00
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
// Only enable the cancel button if the transaction can be aborted
|
|
|
|
this.abortReceiver = (transaction as IAbortable);
|
2007-12-04 20:54:42 +00:00
|
|
|
this.cancelButton.Enabled = (this.abortReceiver != null);
|
2007-07-17 19:33:18 +00:00
|
|
|
|
2008-06-18 19:20:25 +00:00
|
|
|
// Make sure the progress bar control has been created (otherwise, we've got
|
|
|
|
// a chance that BeginInvoke() would fail if the first progress notification
|
|
|
|
// arrived before we called ShowDialog()!)
|
2008-10-29 20:50:55 +00:00
|
|
|
{ IntPtr tempDummy = this.progressBar.Handle; }
|
2008-12-14 18:06:31 +00:00
|
|
|
|
|
|
|
// Subscribe the form to the transaction it is supposed to monitor.
|
|
|
|
// Careful: With the new design, this can cause the asyncEndedDelegate()
|
2008-10-29 20:50:55 +00:00
|
|
|
// callback to be called immediately and synchronously!
|
2008-12-14 18:06:31 +00:00
|
|
|
transaction.AsyncEnded += this.asyncEndedDelegate;
|
|
|
|
IProgressReporter progressReporter = transaction as IProgressReporter;
|
2019-02-01 20:06:36 +00:00
|
|
|
if(progressReporter != null) {
|
2008-03-27 18:34:17 +00:00
|
|
|
progressReporter.AsyncProgressChanged += this.asyncProgressChangedDelegate;
|
2019-02-01 20:06:36 +00:00
|
|
|
}
|
2007-07-17 19:33:18 +00:00
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
// The transaction might have ended before this line was reached, if that's
|
2007-07-17 19:33:18 +00:00
|
|
|
// the case, we don't show the dialog at all.
|
2008-12-14 18:06:31 +00:00
|
|
|
if(!transaction.Ended)
|
2007-07-17 19:33:18 +00:00
|
|
|
ShowDialog();
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
// We're done, unsubscribe from the transaction's events again
|
|
|
|
progressReporter = transaction as IProgressReporter;
|
|
|
|
if(progressReporter != null) {
|
2008-03-27 18:34:17 +00:00
|
|
|
progressReporter.AsyncProgressChanged -= this.asyncProgressChangedDelegate;
|
2008-12-14 18:06:31 +00:00
|
|
|
}
|
|
|
|
transaction.AsyncEnded -= this.asyncEndedDelegate;
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Called when the transaction has ended</summary>
|
|
|
|
/// <param name="sender">Transaction that has ended</param>
|
2007-07-17 19:33:18 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
private void asyncEnded(object sender, EventArgs arguments) {
|
|
|
|
|
|
|
|
// 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)
|
2007-07-22 21:55:35 +00:00
|
|
|
if(Interlocked.Increment(ref this.state) == 2) {
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
// Close the dialog. Ensure the Close() method is invoked from the
|
|
|
|
// same thread the dialog was created in.
|
2008-12-14 18:06:31 +00:00
|
|
|
if(InvokeRequired) {
|
2007-07-17 19:33:18 +00:00
|
|
|
Invoke(new MethodInvoker(Close));
|
2008-12-14 18:06:31 +00:00
|
|
|
} else {
|
2007-07-17 19:33:18 +00:00
|
|
|
Close();
|
2008-12-14 18:06:31 +00:00
|
|
|
}
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Called when the tracked transaction's progress updates</summary>
|
|
|
|
/// <param name="sender">Transaction whose progress has been updated</param>
|
2007-07-17 19:33:18 +00:00
|
|
|
/// <param name="arguments">
|
2008-12-14 18:06:31 +00:00
|
|
|
/// Contains the new progress achieved by the transaction
|
2007-07-17 19:33:18 +00:00
|
|
|
/// </param>
|
2008-03-27 18:34:17 +00:00
|
|
|
private void asyncProgressChanged(object sender, ProgressReportEventArgs arguments) {
|
2007-12-04 20:54:42 +00:00
|
|
|
|
|
|
|
// See if this is the first progress update we're receiving. If yes, we need to
|
|
|
|
// switch the progress bar from marquee into its normal mode!
|
|
|
|
int haveProgress = Interlocked.Exchange(ref this.areProgressUpdatesIncoming, 1);
|
|
|
|
if(haveProgress == 0) {
|
|
|
|
this.progressBar.BeginInvoke(
|
|
|
|
(MethodInvoker)delegate() { this.progressBar.Style = ProgressBarStyle.Blocks; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the new progress to the progress bar
|
2007-07-22 21:55:35 +00:00
|
|
|
this.progressBar.AsyncSetValue(arguments.Progress);
|
2007-12-04 20:54:42 +00:00
|
|
|
|
2007-07-17 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// One-time timer callback that ensurs the form doesn't stay open when the
|
|
|
|
/// close request arrives at an inappropriate time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">Timer that has ticked</param>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
private void controlCreationTimerTicked(object sender, EventArgs arguments) {
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
// This timer is intended to run only once to find out when the dialog has
|
|
|
|
// been fully constructed and is running its message pump. So we'll disable
|
|
|
|
// it as soon as it has been triggered once.
|
|
|
|
this.controlCreationTimer.Enabled = false;
|
|
|
|
|
|
|
|
// 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!
|
2008-12-14 18:06:31 +00:00
|
|
|
if(Interlocked.Increment(ref this.state) == 2) {
|
2007-07-17 19:33:18 +00:00
|
|
|
Close();
|
2008-12-14 18:06:31 +00:00
|
|
|
}
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Aborts the background operation when the user clicks the cancel button
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">Button that has been clicked</param>
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <param name="arguments">Not used</param>
|
|
|
|
private void cancelClicked(object sender, EventArgs arguments) {
|
2007-07-17 19:33:18 +00:00
|
|
|
|
|
|
|
if(this.abortReceiver != null) {
|
|
|
|
|
2007-12-04 20:54:42 +00:00
|
|
|
// Do this first because the abort receiver might trigger the AsyncEnded()
|
|
|
|
// event in the calling thread (us!) and thus destroy our window even in
|
2007-07-17 19:33:18 +00:00
|
|
|
// the safe and synchronous UI thread :)
|
|
|
|
this.cancelButton.Enabled = false;
|
|
|
|
|
|
|
|
// Now we're ready to abort!
|
|
|
|
this.abortReceiver.AsyncAbort();
|
|
|
|
this.abortReceiver = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Delegate for the asyncEnded() method</summary>
|
|
|
|
private EventHandler asyncEndedDelegate;
|
|
|
|
/// <summary>Delegate for the asyncProgressUpdated() method</summary>
|
2008-03-27 18:34:17 +00:00
|
|
|
private EventHandler<ProgressReportEventArgs> asyncProgressChangedDelegate;
|
2007-07-17 19:33:18 +00:00
|
|
|
/// <summary>Whether the form can be closed and should be closed</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// 0: Nothing happened yet
|
|
|
|
/// 1: Ready to close or close requested
|
|
|
|
/// 2: Ready to close and close requested, triggers close
|
|
|
|
/// </remarks>
|
|
|
|
private int state;
|
2008-12-14 18:06:31 +00:00
|
|
|
/// <summary>Whether we're receiving progress updates from the transaction</summary>
|
2007-12-04 20:54:42 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// 0: No progress updates have arrived so far
|
2008-12-14 18:06:31 +00:00
|
|
|
/// 1: We have received at least one progress update from the transaction
|
2007-12-04 20:54:42 +00:00
|
|
|
/// </remarks>
|
|
|
|
private int areProgressUpdatesIncoming;
|
2007-07-17 19:33:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// If set, reference to an object implementing IAbortable by which the
|
|
|
|
/// ongoing background process can be aborted.
|
|
|
|
/// </summary>
|
|
|
|
private IAbortable abortReceiver;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Windows.Forms
|