CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 line
11KB

  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. namespace CNAS_DBSync
  5. {
  6. public partial class frmPrBar : Form
  7. {
  8. /// <summary>
  9. /// 显示百分比(例:30%)或完成数(例:138/2000)
  10. /// </summary>
  11. private static bool _ShowPercentage;
  12. /// <summary>
  13. /// 构造函数
  14. /// </summary>
  15. /// <param name="title">窗体标题</param>
  16. /// <param name="customText">滚动条上方显示的文字提示</param>
  17. /// <param name="taskCount">总任务数</param>
  18. /// <param name="showPercentage">显示百分比(例:30%)或完成数(例:138/2000)</param>
  19. /// <param name="showClose">是否显示关闭按钮</param>
  20. /// <param name="showCustomButton">是否显示开始/暂停/继续/停止按钮</param>
  21. public frmPrBar(string title, string customText, int taskCount, bool showPercentage, bool showClose, bool showCustomButton)
  22. {
  23. InitializeComponent();
  24. this.Text = title;
  25. SetText(customText);
  26. //pictureBoxCancel.Visible = showCustomClose;
  27. _ShowPercentage = showPercentage;
  28. this.ControlBox = showClose;
  29. btnStart.Visible = showCustomButton;
  30. btnPause.Visible = showCustomButton;
  31. btnGoon.Visible = showCustomButton;
  32. btnStop.Visible = showCustomButton;
  33. ProgressBarService.Status = ProgressBarRunStatus.None;
  34. this.progressBar1.Maximum = taskCount;
  35. this.progressBar1.Value = 0;
  36. }
  37. protected override void WndProc(ref Message m)
  38. {
  39. const int WM_SYSCOMMAND = 0x0112;
  40. const int SC_CLOSE = 0xF060;
  41. if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
  42. {
  43. //Application.ExitThread();
  44. //ProgressBarService.CloseBarForm();
  45. ProgressBarService.Status = ProgressBarRunStatus.Close;
  46. return;
  47. }
  48. base.WndProc(ref m);
  49. }
  50. /// <summary>
  51. /// 委托修改等待窗显示的文字
  52. /// </summary>
  53. /// <param name="text"></param>
  54. private delegate void SetTextHandler(string text);
  55. public void SetText(string text)
  56. {
  57. if (lblMsg.InvokeRequired)
  58. this.Invoke(new SetTextHandler(SetText), text);
  59. else
  60. {
  61. if (text.ToUpper() == "CLOSE")
  62. {
  63. this.Close();
  64. this.Dispose();
  65. }
  66. else
  67. lblMsg.Text = text;
  68. }
  69. }
  70. private delegate void UpdateUI(int step);
  71. /// <summary>
  72. /// 更新UI
  73. /// </summary>
  74. /// <param name="step"></param>
  75. public void UpdataUIStatus(int step)
  76. {
  77. try
  78. {
  79. if (this.IsDisposed || !this.IsHandleCreated)
  80. return;
  81. if (InvokeRequired)
  82. {
  83. this.Invoke(new UpdateUI(delegate (int s)
  84. {
  85. this.progressBar1.Value = s;
  86. //this.progressBar1.Value += s;
  87. if (_ShowPercentage)
  88. this.lblCount.Text = (this.progressBar1.Value * 100 / this.progressBar1.Maximum).ToString() + "%";
  89. else
  90. this.lblCount.Text = this.progressBar1.Value.ToString() + "/" + this.progressBar1.Maximum.ToString();
  91. if (this.progressBar1.Value == this.progressBar1.Maximum)
  92. this.Close();
  93. }), step);
  94. }
  95. else
  96. {
  97. if (this.progressBar1.Value >= this.progressBar1.Maximum)
  98. this.progressBar1.Value = 0;
  99. this.progressBar1.Value = step;
  100. //this.progressBar1.Value += step;
  101. if (_ShowPercentage)
  102. this.lblCount.Text = (this.progressBar1.Value * 100 / this.progressBar1.Maximum).ToString() + "%";
  103. else
  104. this.lblCount.Text = this.progressBar1.Value.ToString() + "/" + this.progressBar1.Maximum.ToString();
  105. }
  106. }
  107. catch (Exception ex) { }
  108. }
  109. /// <summary>
  110. /// 开始
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void btnStart_Click(object sender, EventArgs e)
  115. {
  116. ProgressBarService.Status = ProgressBarRunStatus.Start;
  117. btnStart.Enabled = false;
  118. btnPause.Enabled = true;
  119. btnGoon.Enabled = true;
  120. btnStop.Enabled = true;
  121. this.ControlBox = false;
  122. }
  123. /// <summary>
  124. /// 暂停
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void btnPause_Click(object sender, EventArgs e)
  129. {
  130. //ProgressBarService.PauseProgress();
  131. ProgressBarService.Status = ProgressBarRunStatus.Pause;
  132. btnStart.Enabled = false;
  133. btnPause.Enabled = false;
  134. btnGoon.Enabled = true;
  135. btnStop.Enabled = true;
  136. this.ControlBox = false;
  137. }
  138. /// <summary>
  139. /// 继续
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void btnGoon_Click(object sender, EventArgs e)
  144. {
  145. ProgressBarService.Status = ProgressBarRunStatus.GoOn;
  146. btnStart.Enabled = false;
  147. btnPause.Enabled = true;
  148. btnGoon.Enabled = false;
  149. btnStop.Enabled = true;
  150. this.ControlBox = false;
  151. }
  152. /// <summary>
  153. /// 停止
  154. /// </summary>
  155. /// <param name="sender"></param>
  156. /// <param name="e"></param>
  157. private void btnStop_Click(object sender, EventArgs e)
  158. {
  159. ProgressBarService.Status = ProgressBarRunStatus.Stop;
  160. btnStart.Enabled = true;
  161. btnPause.Enabled = false;
  162. btnGoon.Enabled = false;
  163. btnStop.Enabled = false;
  164. this.ControlBox = true;
  165. }
  166. }
  167. public class ProgressBarService
  168. {
  169. private Thread thred = null;
  170. private frmPrBar bar = null;
  171. public ProgressBarService() { }
  172. private static ProgressBarService _instance;
  173. private static readonly Object syncLock = new Object();
  174. public static ProgressBarService Instance
  175. {
  176. get
  177. {
  178. if (ProgressBarService._instance == null)
  179. {
  180. lock (syncLock)
  181. {
  182. if (ProgressBarService._instance == null)
  183. {
  184. ProgressBarService._instance = new ProgressBarService();
  185. }
  186. }
  187. }
  188. return ProgressBarService._instance;
  189. }
  190. }
  191. #region 创建等待窗口
  192. /// <summary>
  193. /// 创建等待窗口并写入提示文字
  194. /// </summary>
  195. /// <param name="title">标题</param>
  196. /// <param name="str">提示文字</param>
  197. /// <param name="Count">步长</param>
  198. /// <param name="showPercentage">显示百分比(例:30%)或完成数(例:138/2000).默认false显示完成数</param>
  199. /// <param name="showClose">是否显示关闭窗口按钮(true显示;false隐藏),不传值默认fasle</param>
  200. /// <param name="showCustomButton">是否显示开始/暂停/继续/停止按钮(true显示;false隐藏),不传值默认fasle</param>
  201. public static void CreateBarForm(string title, string text, int Count, bool showPercentage = false, bool showClose = false, bool showCustomButton = false)
  202. {
  203. ProgressBarService.Instance.CreateForm(title, text, Count, showPercentage, showClose, showCustomButton);
  204. }
  205. int tid1 = 0;
  206. private void CreateForm(string title, string text, int Count, bool showPercentage, bool showClose, bool showCustomButton)
  207. {
  208. int tid2 = Thread.CurrentThread.ManagedThreadId;
  209. if (tid1 == tid2)
  210. return;
  211. tid1 = tid2;
  212. if (thred != null)
  213. {
  214. try
  215. {
  216. thred = null;
  217. bar = null;
  218. }
  219. catch (Exception)
  220. {
  221. }
  222. }
  223. thred = new Thread(new ThreadStart(delegate ()
  224. {
  225. bar = new frmPrBar(title, text, Count, showPercentage, showClose, showCustomButton);
  226. System.Windows.Forms.Application.Run(bar);
  227. }));
  228. thred.Start();
  229. }
  230. #endregion
  231. #region 关闭等待窗口
  232. /// <summary>
  233. /// 关闭窗口
  234. /// </summary>
  235. public static void CloseBarForm()
  236. {
  237. ProgressBarService.Instance.CloseForm();
  238. }
  239. private void CloseForm()
  240. {
  241. tid1 = 0;
  242. if (thred != null)
  243. {
  244. try
  245. {
  246. bar.SetText("CLOSE");
  247. thred = null;
  248. bar = null;
  249. }
  250. catch (Exception) { }
  251. }
  252. }
  253. #endregion
  254. #region 修改等待窗体的提示文字
  255. /// <summary>
  256. /// 修改提示文字
  257. /// </summary>
  258. /// <param name="text">提示文字</param>
  259. public static void SetBarFormCaption(string text)
  260. {
  261. ProgressBarService.Instance.SetFormCaption(text);
  262. }
  263. private void SetFormCaption(string text)
  264. {
  265. if (bar != null)
  266. {
  267. try
  268. {
  269. bar.SetText(text);
  270. }
  271. catch (Exception) { }
  272. }
  273. }
  274. #endregion
  275. #region 更新进度条
  276. /// <summary>
  277. /// 更新进度
  278. /// </summary>
  279. /// <param name="step"></param>
  280. public static void UpdateProgress(int step)
  281. {
  282. ProgressBarService.Instance.SetProgress(step);
  283. }
  284. private void SetProgress(int step)
  285. {
  286. if (bar != null)
  287. {
  288. try
  289. {
  290. bar.UpdataUIStatus(step);
  291. }
  292. catch (Exception) { }
  293. }
  294. }
  295. #endregion
  296. #region 进度条运行状态
  297. public static ProgressBarRunStatus Status { get; set; }
  298. #endregion
  299. }
  300. public enum ProgressBarRunStatus
  301. {
  302. /// <summary>
  303. /// 无操作
  304. /// </summary>
  305. None,
  306. /// <summary>
  307. /// 开始
  308. /// </summary>
  309. Start,
  310. /// <summary>
  311. /// 暂停
  312. /// </summary>
  313. Pause,
  314. /// <summary>
  315. /// 继续
  316. /// </summary>
  317. GoOn,
  318. /// <summary>
  319. /// 停止
  320. /// </summary>
  321. Stop,
  322. /// <summary>
  323. /// 关闭
  324. /// </summary>
  325. Close
  326. }
  327. }