Splasher.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace IntelligentControlForsx.Start
  10. {
  11. public class Splasher
  12. {
  13. private static Form m_SplashForm = null;
  14. private static ISplashForm m_SplashInterface = null;
  15. private static Thread m_SplashThread = null;
  16. private static string m_TempStatus = string.Empty;
  17. /// <summary>
  18. /// Show the SplashForm
  19. /// </summary>
  20. public static void Show(Type splashFormType)
  21. {
  22. if (m_SplashThread != null)
  23. return;
  24. if (splashFormType == null)
  25. {
  26. throw (new Exception("splashFormType is null"));
  27. }
  28. m_SplashThread = new Thread(new ThreadStart(delegate()
  29. {
  30. CreateInstance(splashFormType);
  31. Application.Run(m_SplashForm);
  32. }));
  33. m_SplashThread.IsBackground = true;
  34. m_SplashThread.SetApartmentState(ApartmentState.STA);
  35. m_SplashThread.Start();
  36. }
  37. /// <summary>
  38. /// set the loading Status
  39. /// </summary>
  40. public static string Status
  41. {
  42. set
  43. {
  44. if (m_SplashInterface == null || m_SplashForm == null)
  45. {
  46. m_TempStatus = value;
  47. return;
  48. }
  49. m_SplashForm.Invoke(
  50. new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),
  51. new object[] { value }
  52. );
  53. }
  54. }
  55. /// <summary>
  56. /// Colse the SplashForm
  57. /// </summary>
  58. public static void Close()
  59. {
  60. if (m_SplashThread == null || m_SplashForm == null) return;
  61. try
  62. {
  63. m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));
  64. }
  65. catch (Exception)
  66. {
  67. }
  68. m_SplashThread = null;
  69. m_SplashForm = null;
  70. }
  71. private static void CreateInstance(Type FormType)
  72. {
  73. object obj = FormType.InvokeMember(null,
  74. BindingFlags.DeclaredOnly |
  75. BindingFlags.Public | BindingFlags.NonPublic |
  76. BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
  77. m_SplashForm = obj as Form;
  78. m_SplashInterface = obj as ISplashForm;
  79. m_SplashForm.StartPosition = FormStartPosition.CenterScreen;
  80. if (m_SplashForm == null)
  81. {
  82. throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));
  83. }
  84. if (m_SplashInterface == null)
  85. {
  86. throw (new Exception("must implement interface ISplashForm"));
  87. }
  88. if (!string.IsNullOrEmpty(m_TempStatus))
  89. m_SplashInterface.SetStatusInfo(m_TempStatus);
  90. }
  91. private delegate void SplashStatusChangedHandle(string newStatusInfo);
  92. }
  93. }