在进入程序主界面时,某些情况下主界面的初始化会消耗很长时间,例如一些复杂的业务系统,可能会从服务器上下载最新的数据进行展示等等,在这种情况下,我们可以采用一个进度界面展示“系统正在加载...”,等主界面加载完之后,进度界面退出,主界面展示出来。
在Program.cs中进行操作:
实例包含三个窗体:主窗体(FrmMain)、登陆窗体(FrmLogin)、进度窗体(SplashScreen1)
using System; using System.Windows.Forms; using DevExpress.LookAndFeel; using System.Threading; using System.Reflection; using System.Configuration; namespace MainUSL { static class Program { private static ApplicationContext context; private static FrmMain mForm; private static Thread oThread; private static SplashScreen1 sForm; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { //DefaultLookAndFeel defaultLookAndFeel1 = new DefaultLookAndFeel(); //defaultLookAndFeel1.LookAndFeel.SetSkinStyle("Office 2010 Blue"); //string skinName = ConfigurationManager.AppSettings["ThemeSkin"].ToString(); //UserLookAndFeel.Default.SkinName = skinName; UserLookAndFeel.Default.SkinName = "DevExpress Dark Style"; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); bool bMutexCreated = true; using (new Mutex(true, Assembly.GetExecutingAssembly().FullName, out bMutexCreated)) { if (!bMutexCreated) { DevExpress.XtraEditors.XtraMessageBox.Show("已经存在一个实例在运行!"); return; } FrmLogin frmLogin = new FrmLogin(); if (frmLogin.ShowDialog() == DialogResult.OK) { DoStartup(); } } //Application.Run(new FrmLogin()); //Application.Run(new FrmMain()); } static void DoStartup() { try { sForm = new SplashScreen1(); //新建一个线程 oThread = new Thread(new ThreadStart(ShowFlash)); //设置线程级别 oThread.Priority = ThreadPriority.Lowest; //后台线程 oThread.IsBackground = true; //启动flash oThread.Start(); //事件订阅(在线程结束时调用OnAppIdle方法) Application.Idle += new EventHandler(OnAppIdle); mForm = new FrmMain(); //Application.Run(mForm); context = new ApplicationContext(); Application.Run(context); } catch (Exception e) { CommonOperator.ExceptionClass.RecordException(e.ToString()); Application.Exit(); } } /// <summary> /// 线程结束后事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnAppIdle(object sender, EventArgs e) { try { if (context.MainForm == null) { //取消事件订阅 Application.Idle -= new EventHandler(OnAppIdle); //标识状态 //mForm.PreLoad(); //为当前线程设置主窗体 context.MainForm = mForm; //启动主界面 Thread.Sleep(5000); context.MainForm.Show(); //终止flash oThread.Abort(); GC.Collect(); Application.DoEvents(); } } catch (Exception ex) { CommonOperator.ExceptionClass.RecordException(ex.ToString()); } } static void ShowFlash() { sForm.ShowDialog(); } } }