Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IProgress<int> and IProgress<string> to ProgressDialog #12

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Ookii.Dialogs.WinForms/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Ookii.Dialogs.WinForms
/// </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 @@ -54,6 +54,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 @@ -530,6 +531,24 @@ public void ShowDialog(IWin32Window owner, object argument)
RunProgressDialog(owner == null ? NativeMethods.GetActiveWindow() : 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 @@ -580,6 +599,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