Skip to content

Commit

Permalink
Added IProgress<int> and IProgress<string> to ProgressDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
vpenades authored and augustoproiete committed Mar 25, 2020
1 parent 2fd92f9 commit f292222
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Ookii.Dialogs.Wpf/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Ookii.Dialogs.Wpf
/// </remarks>
/// <threadsafety static="true" instance="false" />
[DefaultEvent("DoWork"), DefaultProperty("Text"), Description("Represents a dialog that can be used to report progress to the user.")]
public partial class ProgressDialog : Component
public partial class ProgressDialog : Component , IProgress<int>, IProgress<string>
{
private class ProgressChangedData
{
Expand All @@ -42,6 +42,7 @@ private class ProgressChangedData
private bool _useCompactPathsForDescription;
private SafeModuleHandle _currentAnimationModuleHandle;
private bool _cancellationPending;
private int _percentProgress;

/// <summary>
/// Event raised when the dialog is displayed.
Expand Down Expand Up @@ -518,6 +519,24 @@ public void ShowDialog(Window owner, object argument)
RunProgressDialog(owner == null ? NativeMethods.GetActiveWindow() : new WindowInteropHelper(owner).Handle, argument);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
/// <param name="value">The percentage, from 0 to 100, of the operation that is complete.</param>
void IProgress<int>.Report(int value)
{
ReportProgress(value, null, null, null);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
/// <param name="value">The new value of the progress dialog's primary text message, or <see langword="null"/> to leave the value unchanged.</param>
void IProgress<string>.Report(string value)
{
ReportProgress(_percentProgress, value, null, null);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
Expand Down Expand Up @@ -568,6 +587,10 @@ public void ReportProgress(int percentProgress, string text, string description,
throw new ArgumentOutOfRangeException("percentProgress");
if( _dialog == null )
throw new InvalidOperationException(Properties.Resources.ProgressDialogNotRunningError);

// we need to cache the latest percentProgress so IProgress<string>.Report(text) can report the percent progress correctly.
_percentProgress = percentProgress;

_backgroundWorker.ReportProgress(percentProgress, new ProgressChangedData() { Text = text, Description = description, UserState = userState });
}

Expand Down

0 comments on commit f292222

Please sign in to comment.