📄 progress.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ProgressDialog
{
/// <summary>
/// This defines an interface which can be implemented by UI elements
/// which indicate the progress of a long operation.
/// (See ProgressWindow for a typical implementation)
/// </summary>
internal interface IProgressCallback
{
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, without setting the range
/// </summary>
void Begin();
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback.
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
void Begin(int minimum, int maximum);
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, and control whether or not the
/// user can cancel the operation.
/// </summary>
void Begin(bool AllowCancel);
/// <summary>
/// Call this method from the worker thread to finalize the progress meter
/// </summary>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void End();
/// <summary>
/// Call this method from the worker thread to update the form caption.
/// </summary>
/// <param name="text">The caption text to display</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void SetCaption(String text);
/// <summary>
/// Call this method from the worker thread to update the progress text.
/// </summary>
/// <param name="text">The progress text to display</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void SetText(String text);
/// <summary>
/// Call this method from the worker thread to reset the range in the progress callback
/// for the primary progress bar.
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void SetRange_Primary(int minimum, int maximum);
/// <summary>
/// Call this method from the worker thread to increase the progress counter by a specified value
/// for the primary progress bar.
/// </summary>
/// <param name="val">The amount by which to increment the progress indicator</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void StepTo_Primary(int val);
/// <summary>
/// Call this method from the worker thread to step the progress meter to a particular value
/// for the primary progress bar.
/// </summary>
/// <param name="val">The value to which to step the meter</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void Increment_Primary(int val);
/// <summary>
/// Call this method from the worker thread to reset the range in the progress callback
/// for the secondary progress bar.
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void SetRange_Secondary(int minimum, int maximum);
/// <summary>
/// Call this method from the worker thread to increase the progress counter by a specified value
/// for the secondary progress bar.
/// </summary>
/// <param name="val">The amount by which to increment the progress indicator</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void StepTo_Secondary(int val);
/// <summary>
/// Call this method from the worker thread to step the progress meter to a particular value
/// for the secondary progress bar.
/// </summary>
/// <param name="val">The value to which to step the meter</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
void Increment_Secondary(int val);
/// <summary>
/// If this property is true, then you should abort work
/// </summary>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
bool IsAborting
{
get;
}
bool IsInitialized
{
get;
}
int Stages
{
get;
}
}
/// <summary>
/// Summary description for ProgressWindow.
/// </summary>
internal class ProgressWindow : System.Windows.Forms.Form, IProgressCallback
{
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ProgressBar prgPrimary;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public delegate void SetTextInvoker(String text);
public delegate void IncrementInvoker(int val);
public delegate void StepToInvoker(int val);
public delegate void RangeInvoker(int minimum, int maximum);
public delegate void CancelInvoker(bool AllowCancel);
private System.Threading.ManualResetEvent initEvent = new System.Threading.ManualResetEvent(false);
private System.Threading.ManualResetEvent abortEvent = new System.Threading.ManualResetEvent(false);
private String titleRoot = "";
private bool requiresClose = true;
private bool initialized = false;
public ProgressWindow()
{
// Required for Windows Form Designer support
InitializeComponent();
}
#region Implementation of IProgressCallback
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, without setting the range
/// </summary>
public void Begin()
{
initEvent.WaitOne();
Invoke(new MethodInvoker(DoBegin));
}
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress meter.
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
public void Begin(int minimum, int maximum)
{
initEvent.WaitOne();
Invoke(new RangeInvoker(DoBegin), new object[] { minimum, maximum });
}
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress meter.
/// </summary>
/// <param name="AllowCancel">A boolean value that determines whether or not the cancel button is displayed.</param>
public void Begin(bool AllowCancel)
{
initEvent.WaitOne();
Invoke(new CancelInvoker(DoBegin), new object[] { AllowCancel });
}
/// <summary>
/// Call this method from the worker thread to finalize the progress meter
/// </summary>
public void End()
{
if (requiresClose)
{
Invoke(new MethodInvoker(DoEnd));
}
}
/// <summary>
/// Call this method from the worker thread to update the form's caption.
/// </summary>
/// <param name="text">The caption text to display</param>
public void SetCaption(String text)
{
Invoke(new SetTextInvoker(DoSetCaption), new object[] { text });
}
/// <summary>
/// Call this method from the worker thread to update the progress text.
/// </summary>
/// <param name="text">The progress text to display</param>
public void SetText(String text)
{
Invoke(new SetTextInvoker(DoSetText), new object[] { text });
}
/// <summary>
/// Call this method from the worker thread to reset the range in the progress callback
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
public void SetRange_Primary(int minimum, int maximum)
{
initEvent.WaitOne();
Invoke(new RangeInvoker(DoSetRange_Primary), new object[] { minimum, maximum });
}
/// <summary>
/// Call this method from the worker thread to increase the progress counter by a specified value.
/// </summary>
/// <param name="val">The amount by which to increment the progress indicator</param>
public void Increment_Primary(int val)
{
Invoke(new IncrementInvoker(DoIncrement), new object[] { val });
}
/// <summary>
/// Call this method from the worker thread to step the progress meter to a particular value.
/// </summary>
/// <param name="val"></param>
public void StepTo_Primary(int val)
{
Invoke(new StepToInvoker(DoStepTo), new object[] { val });
}
/// <summary>
/// Call this method from the worker thread to reset the range in the progress callback
/// for the secondary progressbar
/// </summary>
/// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
public void SetRange_Secondary(int minimum, int maximum) { return; }
/// <summary>
/// Call this method from the worker thread to increase the progress counter by a specified value.
/// </summary>
/// <param name="val">The amount by which to increment the progress indicator</param>
public void Increment_Secondary(int val) { return; }
/// <summary>
/// Call this method from the worker thread to step the progress meter to a particular value.
/// </summary>
/// <param name="val"></param>
public void StepTo_Secondary(int val) { return; }
/// <summary>
/// If this property is true, then you should abort work
/// </summary>
public bool IsAborting
{
get { return abortEvent.WaitOne(0, false); }
}
public bool IsInitialized
{
get { return initialized; }
}
public int Stages { get { return 1; } }
#endregion
#region Implementation members invoked on the owner thread
private void DoBegin()
{
titleRoot = Text;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -