Windows Form经常会在启动主界面的时候预先有启动画面,这也是因为用户体验的需要,用户知道已经启动application,而不是在load resource的时候等待。因此这里不能用单线程的思路,单单只是设计一个界面而已,而需要在splash画面的时候同时Load resource。那么这个技术有两个线程,一个是splash画面,二是load resource。搜了一些资料,下面进行一些总结:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading; namespace WindowsTest { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Thread thUI = new Thread(new ThreadStart(ShowSplashWindow)); thUI.Name = "Splash UI"; thUI.Priority = ThreadPriority.Normal; thUI.Start(); Thread th = new Thread(new ThreadStart(LoadResources)); th.Name = "Resource Loader"; //th.Priority = ThreadPriority.Highest; th.Priority = ThreadPriority.Normal; th.Start(); th.Join(); if (splashForm != null) { splashForm.Invoke(new MethodInvoker(delegate { splashForm.Close(); })); } thUI.Join(); Application.Run(new MainForm()); } public static SplashForm splashForm { get; set; } private static void ShowSplashWindow() { splashForm = new SplashForm(); Application.Run(splashForm); } private static void LoadResources() { ; i <= ; i++) { if (splashForm != null) { splashForm.Invoke(new MethodInvoker(delegate { splashForm.label1.Text = "Loading some things... " + DateTime.Now.ToString(); })); } Thread.Sleep(); } splashForm.Invoke(new MethodInvoker(delegate { splashForm.label1.Text = "Done. " + DateTime.Now.ToString(); })); } } }
这段代码的问题在于画面启动完主界面是最小化了,不解为何如此
http://www.sufeinet.com/forum.php?mod=viewthread&action=printable&tid=2697
这里的代码比较成熟了,我做了一些小小的改变。用ApplicationContext来解决splash form和main form之间的转换,而在Program的类的InitApp function里如果要改变splash from的label1则需要Invoke来异步调用SplashFrom的PrintMsg function,否则会卡卡面。在SplashForm_Load函数里用到了线程池技术,这里不能用invoke,因为invoke必须使用类实例,而Program是static类,InitApp也是static的,在InitApp里可以用线程池来实现。另外贴下线程池的适用场合
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading; namespace WindowsTest { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MyApplicationContent appContent = new MyApplicationContent(new MainForm(), new SplashForm()); Application.Run(appContent); } //模拟耗时操作,这里假调程序需要访问网络来实现登录验证 //这是一个耗时操作,我们需要在执行的过程中,向用户实时显示一些信息 //那么,多线程是唯一的解决方案,在主线程执行这些,界面会死掉的 public static void InitApp(Object parm) { SplashForm startup = parm as SplashForm; startup.Invoke(new UiThreadProc(startup.PrintMsg), "正在初始化..."); Thread.Sleep(); startup.Invoke(new UiThreadProc(startup.PrintMsg), "正在验证用户身份信息..."); Thread.Sleep(); startup.Invoke(new UiThreadProc(startup.PrintMsg), "用户身份验证成功。"); Thread.Sleep(); startup.Invoke(new UiThreadProc(startup.PrintMsg), "正在登录..."); Thread.Sleep(); bool loginSuccess = true; //这里可以根据执行的结果判断,如果登录失败就退出程序,否则显示主窗体 if (loginSuccess) { startup.Invoke(new UiThreadProc(startup.PrintMsg), "登录成功,欢迎使用!"); Thread.Sleep(); startup.Invoke(new UiThreadProc(startup.CloseForm), false); } else { startup.Invoke(new UiThreadProc(startup.CloseForm), true); } } } public delegate void UiThreadProc(Object o); //WinForm里,默认第一个创建的窗体是主窗体,所以需要用应用程序域来管理 public class MyApplicationContent : ApplicationContext { private Form realMainForm; public MyApplicationContent(Form mainForm, Form flashForm) : base(mainForm) { this.realMainForm = mainForm; this.MainForm = flashForm; } protected override void OnMainFormClosed(object sender, EventArgs e) { if (sender is SplashForm) { SplashForm splashForm = sender as SplashForm; if (!splashForm.Exit) { this.MainForm = realMainForm; realMainForm.Show(); } else { base.OnMainFormClosed(sender, e); } } else { base.OnMainFormClosed(sender, e); } } } }
SplashForm.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsTest { public partial class SplashForm : Form { private bool exit; public bool Exit { get { return exit; } } public SplashForm() { InitializeComponent(); } //显示文字信息 public void PrintMsg(Object msg) { label1.Text = msg.ToString(); } //关闭启动窗体,如果需要中止程序,传参数false public void CloseForm(Object o) { this.exit = Convert.ToBoolean(o); this.Close(); } private void SplashForm_Load(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(new WaitCallback(Program.InitApp), this); } } }
只需要将Program.cs里的InitApp里的Thread.Sleep改成实际的load resource代码即可