LoadingHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using prBrowser.Weblogic;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace loadingForm
  9. {
  10. public class LoadingHelper
  11. {
  12. #region 相关变量定义
  13. /// <summary>
  14. /// 定义委托进行窗口关闭
  15. /// </summary>
  16. private delegate void CloseDelegate();
  17. private static loading loadingForm;
  18. private static readonly Object syncLock = new Object(); //加锁使用
  19. #endregion
  20. //private LoadingHelper()
  21. //{
  22. //}
  23. /// <summary>
  24. /// 显示loading框
  25. /// </summary>
  26. public static void ShowLoadingScreen()
  27. {
  28. // Make sure it is only launched once.
  29. if (loadingForm != null)
  30. return;
  31. Thread thread = new Thread(new ThreadStart(LoadingHelper.ShowForm));
  32. thread.IsBackground = true;
  33. thread.SetApartmentState(ApartmentState.STA);
  34. thread.Start();
  35. }
  36. /// <summary>
  37. /// 显示窗口
  38. /// </summary>
  39. private static void ShowForm()
  40. {
  41. if (loadingForm != null)
  42. {
  43. loadingForm.closeOrder();
  44. loadingForm = null;
  45. }
  46. loadingForm = new loading();
  47. loadingForm.TopMost = true;
  48. loadingForm.ShowDialog();
  49. }
  50. /// <summary>
  51. /// 关闭窗口
  52. /// </summary>
  53. public static void CloseForm()
  54. {
  55. Thread.Sleep(50); //可能到这里线程还未起来,所以进行延时,可以确保线程起来,彻底关闭窗口
  56. if (loadingForm != null)
  57. {
  58. lock (syncLock)
  59. {
  60. Thread.Sleep(50);
  61. if (loadingForm != null)
  62. {
  63. Thread.Sleep(50); //通过三次延时,确保可以彻底关闭窗口
  64. loadingForm.Invoke(new CloseDelegate(LoadingHelper.CloseFormInternal));
  65. }
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// 关闭窗口,委托中使用
  71. /// </summary>
  72. private static void CloseFormInternal()
  73. {
  74. loadingForm.closeOrder();
  75. loadingForm = null;
  76. }
  77. }
  78. }