frmLoading.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using ComponentFactory.Krypton.Toolkit;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace NXWMS.Forms
  14. {
  15. public partial class frmLoading : KryptonForm
  16. {
  17. /// <summary>
  18. /// 加载最大.
  19. /// </summary>
  20. public int _LoadMax { get; set; } = 1;
  21. /// <summary>
  22. /// 当前加载.
  23. /// </summary>
  24. private int _CurrenLoad = 0;
  25. /// <summary>
  26. /// 加载轮次数
  27. /// </summary>
  28. public int _LoadNumber { get; set; } = -1;
  29. /// <summary>
  30. /// 当前轮次数
  31. /// </summary>
  32. private int _CurrentNumber = 0;
  33. /// <summary>
  34. /// 加载最后的消息
  35. /// </summary>
  36. private string _Message = "请耐心等待.正在加载中";
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. /// <param name="num1"></param>
  41. /// <param name="num2"></param>
  42. /// <returns></returns>
  43. public delegate string ExecAction();
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. /// <param name="pageSize"></param>
  48. /// <param name="pageIndex"></param>
  49. /// <returns></returns>
  50. public delegate string ExecActionPage(int pageSize, int pageIndex);
  51. /// <summary>
  52. /// 文件加载至数据视图
  53. /// </summary>
  54. /// <param name="dataGrid"></param>
  55. /// <param name="filePath"></param>
  56. /// <returns></returns>
  57. public delegate string ExecActionGridLoad(DataGridView dataGrid,string filePath);
  58. #region 20200907 孙亚龙添加
  59. /// <summary>
  60. /// 定义可以传单个主表主键ID的委托。
  61. /// 用于加载明细表时,也使用当前的加载控件。
  62. /// </summary>
  63. /// <param name="mainTableId">某个业务表的主表主键ID</param>
  64. /// <returns></returns>
  65. public delegate string ExecActionExt1(int mainTableId);
  66. /// <summary>
  67. /// 执行具体的获取明细表信息函数。然后关闭加载控件。
  68. /// </summary>
  69. /// <param name="cal">具体的获取明细表信息函数对象名</param>
  70. /// <param name="mainTableId">某个业务表的主表主键ID</param>
  71. /// <returns></returns>
  72. public string EventCalExecExt1(ExecActionExt1 cal, int mainTableId)
  73. {
  74. Application.DoEvents();
  75. string result;
  76. try
  77. {
  78. result = cal(mainTableId);
  79. }
  80. catch (Exception ex)
  81. {
  82. Close();
  83. return ex.Message;
  84. }
  85. Close();
  86. return result;
  87. }
  88. #endregion
  89. /// <summary>
  90. /// 执行回调函数
  91. /// </summary>
  92. /// <param name="cal"></param>
  93. /// <returns></returns>
  94. public string EventCalExec(ExecAction cal)
  95. {
  96. Application.DoEvents();
  97. string result;
  98. try
  99. {
  100. result = cal();
  101. }
  102. catch (Exception ex)
  103. {
  104. Close();
  105. return ex.Message;
  106. }
  107. Close();
  108. return result;
  109. }
  110. /// <summary>
  111. /// 文件加载数据视图 执行回调函数
  112. /// </summary>
  113. /// <param name="cal"></param>
  114. /// <param name="dataGrid"></param>
  115. /// <param name="filePath"></param>
  116. /// <returns></returns>
  117. public string EventCalExec(ExecActionGridLoad cal, DataGridView dataGrid, string filePath)
  118. {
  119. Application.DoEvents();
  120. string result;
  121. try
  122. {
  123. result = cal(dataGrid, filePath);
  124. }
  125. catch (Exception ex)
  126. {
  127. Close();
  128. return ex.Message;
  129. }
  130. Close();
  131. return result;
  132. }
  133. public static string ExecFun(Func<string> func)
  134. {
  135. var frm = new frmLoading();
  136. frm.Show();
  137. return frm.Exec(func);
  138. }
  139. public string Exec(Func<string> func)
  140. {
  141. Application.DoEvents();
  142. string result;
  143. try
  144. {
  145. result = func();
  146. }
  147. catch (Exception ex)
  148. {
  149. Close();
  150. return ex.Message;
  151. }
  152. Close();
  153. return result;
  154. }
  155. /// <summary>
  156. /// 执行带分页回调函数
  157. /// </summary>
  158. /// <param name="cal"></param>
  159. /// <param name="pageSize"></param>
  160. /// <param name="pageIndex"></param>
  161. /// <returns></returns>
  162. public string EventCalExec(ExecActionPage cal, int pageSize, int pageIndex)
  163. {
  164. Application.DoEvents();
  165. string result;
  166. try
  167. {
  168. result = cal(pageSize, pageIndex);
  169. }
  170. catch (Exception ex)
  171. {
  172. Close();
  173. return ex.Message;
  174. }
  175. Close();
  176. return result;
  177. }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. /// <param name="cal"></param>
  182. /// <returns></returns>
  183. public bool EventCalExecTask(ExecAction cal)
  184. {
  185. var task = new Task(() =>
  186. {
  187. cal();
  188. });
  189. task.Start();
  190. Task cwt = task.ContinueWith(t =>
  191. {
  192. Close();
  193. });
  194. task.Wait();
  195. cwt.Wait();
  196. return true;
  197. }
  198. public string _message
  199. {
  200. get
  201. {
  202. return _Message;
  203. }
  204. set
  205. {
  206. lbMessage.Text = value;
  207. _Message = value;
  208. }
  209. }
  210. public int _loadIntervel
  211. {
  212. get
  213. {
  214. return _loadIntervel;
  215. }
  216. set
  217. {
  218. if (value > 0)
  219. {
  220. timer.Interval = value;
  221. }
  222. else
  223. {
  224. timer.Interval = 50;
  225. }
  226. }
  227. }
  228. public frmLoading()
  229. {
  230. InitializeComponent();
  231. Application.DoEvents();
  232. lbMessage.Text = _Message;
  233. }
  234. public frmLoading(Action action)
  235. {
  236. InitializeComponent();
  237. lbMessage.Text = _Message;
  238. }
  239. private void timer_Tick(object sender, EventArgs e)
  240. {
  241. if (_CurrentNumber >= _LoadNumber && _LoadNumber != -1)
  242. {
  243. Close();
  244. return;
  245. }
  246. if (_CurrenLoad < _LoadMax)
  247. {
  248. lbMessage.Text = _Message + string.Empty.PadRight(_CurrenLoad, '.');
  249. }
  250. else if (_CurrenLoad == _LoadMax)
  251. {
  252. lbMessage.Text = _Message + string.Empty.PadRight(_CurrenLoad, '.');
  253. _CurrentNumber++;
  254. }
  255. else
  256. {
  257. _CurrenLoad = 1;
  258. lbMessage.Text = _Message + string.Empty.PadRight(1, '.');
  259. }
  260. _CurrenLoad++;
  261. }
  262. }
  263. }