等待窗口 or splash 窗体用法

1.建立一个窗体

public partial class StartCompare : Form
{
        /// <summary>
        /// 当前状态
        /// </summary>
        private string _stateText = string.Empty;
        
        /// <summary>
        /// 当前状态
        /// </summary>
        public string StateText
        {
            get { return _stateText; }
            set 
            {
                _stateText = value;
                ChangeStateText();
            }
        }


        public StartCompare()
        {
            InitializeComponent();
        }

        public void ChangeStateText()
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(this.ChangeStateText));
                    return;
                }

                this.lbState.Text = _stateText;
            }
            catch (Exception e)
            {
                //    异常处理
                Logger.LogException(e);
            }

        }
    }

2. splash窗体操作类

 public class Splash
 {
        static StartCompare MySplashForm = null;
        static Thread MySplashThread = null;

        static void ShowThread()
        {
            MySplashForm = new StartCompare();
            Application.Run(MySplashForm);
        }

        static public void Show()
        {
            if (MySplashThread != null)
                return;

            MySplashThread = new Thread(new ThreadStart(Splash.ShowThread));
            MySplashThread.IsBackground = true;
            MySplashThread.ApartmentState = ApartmentState.STA;
            MySplashThread.Start();
        }

        static public void Close()
        {
            if (MySplashThread == null) return;
            if (MySplashForm == null) return;

            try
            {
                MySplashForm.Invoke(new MethodInvoker(MySplashForm.Close));
            }
            catch (Exception)
            {
            }
            MySplashThread = null;
            MySplashForm = null;
        }

        static public string Status
        {
            set
            {
                if (MySplashForm == null)
                {
                    return;
                }

                MySplashForm.StateText = value;
            }
            get
            {
                if (MySplashForm == null)
                {
                    throw new InvalidOperationException("Splash Form not on screen");
                }
                return MySplashForm.StateText;
            }
        }

    }

3.调用

 Splash.Show();
 //
 Splash.Status = "正在初始化版本比对,请稍后……";
 InitializeComponent();
  Splash.Status = "初始化完成,请稍后……";
 Splash.Close();
posted @ 2009-04-01 15:49  herobeast  阅读(489)  评论(0编辑  收藏  举报