123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace IntelligentControlForsx.Start
- {
- public class Splasher
- {
- private static Form m_SplashForm = null;
- private static ISplashForm m_SplashInterface = null;
- private static Thread m_SplashThread = null;
- private static string m_TempStatus = string.Empty;
- /// <summary>
- /// Show the SplashForm
- /// </summary>
- public static void Show(Type splashFormType)
- {
- if (m_SplashThread != null)
- return;
- if (splashFormType == null)
- {
- throw (new Exception("splashFormType is null"));
- }
- m_SplashThread = new Thread(new ThreadStart(delegate()
- {
- CreateInstance(splashFormType);
- Application.Run(m_SplashForm);
- }));
- m_SplashThread.IsBackground = true;
- m_SplashThread.SetApartmentState(ApartmentState.STA);
- m_SplashThread.Start();
- }
- /// <summary>
- /// set the loading Status
- /// </summary>
- public static string Status
- {
- set
- {
- if (m_SplashInterface == null || m_SplashForm == null)
- {
- m_TempStatus = value;
- return;
- }
- m_SplashForm.Invoke(
- new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),
- new object[] { value }
- );
- }
- }
- /// <summary>
- /// Colse the SplashForm
- /// </summary>
- public static void Close()
- {
- if (m_SplashThread == null || m_SplashForm == null) return;
- try
- {
- m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));
- }
- catch (Exception)
- {
- }
- m_SplashThread = null;
- m_SplashForm = null;
- }
- private static void CreateInstance(Type FormType)
- {
- object obj = FormType.InvokeMember(null,
- BindingFlags.DeclaredOnly |
- BindingFlags.Public | BindingFlags.NonPublic |
- BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
- m_SplashForm = obj as Form;
- m_SplashInterface = obj as ISplashForm;
- m_SplashForm.StartPosition = FormStartPosition.CenterScreen;
- if (m_SplashForm == null)
- {
- throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));
- }
- if (m_SplashInterface == null)
- {
- throw (new Exception("must implement interface ISplashForm"));
- }
- if (!string.IsNullOrEmpty(m_TempStatus))
- m_SplashInterface.SetStatusInfo(m_TempStatus);
- }
- private delegate void SplashStatusChangedHandle(string newStatusInfo);
- }
- }
|