/ARG.SystemBase/RuProgressBar/ProgressWindow.cs

http://argo69.codeplex.com · C# · 264 lines · 163 code · 29 blank · 72 comment · 4 complexity · ff3ceb95c93726b89683947b63cf7854 MD5 · raw file

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. namespace RuProgressBar
  7. {
  8. /// <summary>
  9. /// Summary description for ProgressWindow.
  10. /// </summary>
  11. public class ProgressWindow : System.Windows.Forms.Form, IProgressCallback
  12. {
  13. private System.Windows.Forms.ProgressBar progressBar;
  14. /// <summary>
  15. /// Required designer variable.
  16. /// </summary>
  17. private System.ComponentModel.Container components = null;
  18. public delegate void SetTextInvoker(String text);
  19. public delegate void IncrementInvoker(int val);
  20. public delegate void StepToInvoker(int val);
  21. public delegate void RangeInvoker(int minimum, int maximum);
  22. private String titleRoot = "";
  23. private System.Threading.ManualResetEvent initEvent = new System.Threading.ManualResetEvent(false);
  24. private System.Threading.ManualResetEvent abortEvent = new System.Threading.ManualResetEvent(false);
  25. private bool requiresClose = true;
  26. public ProgressWindow()
  27. {
  28. //
  29. // Required for Windows Form Designer support
  30. //
  31. InitializeComponent();
  32. }
  33. #region Implementation of IProgressCallback
  34. /// <summary>
  35. /// Call this method from the worker thread to initialize
  36. /// the progress meter.
  37. /// </summary>
  38. /// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
  39. /// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
  40. public void Begin(int minimum, int maximum)
  41. {
  42. initEvent.WaitOne();
  43. Invoke(new RangeInvoker(DoBegin), new object[] { minimum, maximum });
  44. }
  45. /// <summary>
  46. /// Call this method from the worker thread to initialize
  47. /// the progress callback, without setting the range
  48. /// </summary>
  49. public void Begin()
  50. {
  51. initEvent.WaitOne();
  52. Invoke(new MethodInvoker(DoBegin));
  53. }
  54. /// <summary>
  55. /// Call this method from the worker thread to reset the range in the progress callback
  56. /// </summary>
  57. /// <param name="minimum">The minimum value in the progress range (e.g. 0)</param>
  58. /// <param name="maximum">The maximum value in the progress range (e.g. 100)</param>
  59. /// <remarks>You must have called one of the Begin() methods prior to this call.</remarks>
  60. public void SetRange(int minimum, int maximum)
  61. {
  62. initEvent.WaitOne();
  63. Invoke(new RangeInvoker(DoSetRange), new object[] { minimum, maximum });
  64. }
  65. /// <summary>
  66. /// Call this method from the worker thread to update the progress text.
  67. /// </summary>
  68. /// <param name="text">The progress text to display</param>
  69. public void SetText(String text)
  70. {
  71. Invoke(new SetTextInvoker(DoSetText), new object[] { text });
  72. }
  73. /// <summary>
  74. /// Call this method from the worker thread to increase the progress counter by a specified value.
  75. /// </summary>
  76. /// <param name="val">The amount by which to increment the progress indicator</param>
  77. public void Increment(int val)
  78. {
  79. Invoke(new IncrementInvoker(DoIncrement), new object[] { val });
  80. }
  81. /// <summary>
  82. /// Call this method from the worker thread to step the progress meter to a particular value.
  83. /// </summary>
  84. /// <param name="val"></param>
  85. public void StepTo(int val)
  86. {
  87. Invoke(new StepToInvoker(DoStepTo), new object[] { val });
  88. }
  89. /// <summary>
  90. /// If this property is true, then you should abort work
  91. /// </summary>
  92. public bool IsAborting
  93. {
  94. get
  95. {
  96. return abortEvent.WaitOne(0, false);
  97. }
  98. }
  99. /// <summary>
  100. /// Call this method from the worker thread to finalize the progress meter
  101. /// </summary>
  102. public void End()
  103. {
  104. if (requiresClose)
  105. {
  106. Invoke(new MethodInvoker(DoEnd));
  107. }
  108. }
  109. #endregion
  110. #region Implementation members invoked on the owner thread
  111. private void DoSetText(String text)
  112. {
  113. //label.Text = text;
  114. }
  115. private void DoIncrement(int val)
  116. {
  117. progressBar.Increment(val);
  118. UpdateStatusText();
  119. }
  120. private void DoStepTo(int val)
  121. {
  122. progressBar.Value = val;
  123. UpdateStatusText();
  124. }
  125. private void DoBegin(int minimum, int maximum)
  126. {
  127. DoBegin();
  128. DoSetRange(minimum, maximum);
  129. }
  130. private void DoBegin()
  131. {
  132. ControlBox = true;
  133. }
  134. private void DoSetRange(int minimum, int maximum)
  135. {
  136. progressBar.Minimum = minimum;
  137. progressBar.Maximum = maximum;
  138. progressBar.Value = minimum;
  139. titleRoot = Text;
  140. }
  141. private void DoEnd()
  142. {
  143. Close();
  144. }
  145. #endregion
  146. #region Overrides
  147. /// <summary>
  148. /// Handles the form load, and sets an event to ensure that
  149. /// intialization is synchronized with the appearance of the form.
  150. /// </summary>
  151. /// <param name="e"></param>
  152. protected override void OnLoad(System.EventArgs e)
  153. {
  154. base.OnLoad(e);
  155. ControlBox = false;
  156. initEvent.Set();
  157. }
  158. /// <summary>
  159. /// Clean up any resources being used.
  160. /// </summary>
  161. protected override void Dispose(bool disposing)
  162. {
  163. if (disposing)
  164. {
  165. if (components != null)
  166. {
  167. components.Dispose();
  168. }
  169. }
  170. base.Dispose(disposing);
  171. }
  172. /// <summary>
  173. /// Handler for 'Close' clicking
  174. /// </summary>
  175. /// <param name="e"></param>
  176. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  177. {
  178. requiresClose = false;
  179. AbortWork();
  180. base.OnClosing(e);
  181. }
  182. #endregion
  183. #region Implementation Utilities
  184. /// <summary>
  185. /// Utility function that formats and updates the title bar text
  186. /// </summary>
  187. private void UpdateStatusText()
  188. {
  189. Text = titleRoot + String.Format(" - {0}% complete", (progressBar.Value * 100) / (progressBar.Maximum - progressBar.Minimum));
  190. }
  191. /// <summary>
  192. /// Utility function to terminate the thread
  193. /// </summary>
  194. private void AbortWork()
  195. {
  196. abortEvent.Set();
  197. }
  198. #endregion
  199. #region Windows Form Designer generated code
  200. /// <summary>
  201. /// Required method for Designer support - do not modify
  202. /// the contents of this method with the code editor.
  203. /// </summary>
  204. private void InitializeComponent()
  205. {
  206. this.progressBar = new System.Windows.Forms.ProgressBar();
  207. this.SuspendLayout();
  208. //
  209. // progressBar
  210. //
  211. this.progressBar.Anchor = System.Windows.Forms.AnchorStyles.None;
  212. this.progressBar.Location = new System.Drawing.Point(-3, 1);
  213. this.progressBar.Name = "progressBar";
  214. this.progressBar.Size = new System.Drawing.Size(295, 27);
  215. this.progressBar.TabIndex = 1;
  216. //
  217. // ProgressWindow
  218. //
  219. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  220. this.ClientSize = new System.Drawing.Size(290, 30);
  221. this.ControlBox = false;
  222. this.Controls.Add(this.progressBar);
  223. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  224. this.MaximizeBox = false;
  225. this.MinimizeBox = false;
  226. this.Name = "ProgressWindow";
  227. this.ShowIcon = false;
  228. this.ShowInTaskbar = false;
  229. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  230. this.Text = "ProgressWindow";
  231. this.ResumeLayout(false);
  232. }
  233. #endregion
  234. }
  235. }