使用多线程制作启动窗体和登录窗体

对程序简单说明一下,MainForm是主窗体,需要从数据库读取数据来生成界面,程序运行时,首先显示SplachScreenForm,并在SplachScreenForm中执行MainForm中的生成界面的方法。如果MainForm界面生成成功,则显示LoginForm,否则退出应用程序。LoginForm中,用户登录成功,则显示MainForm,取消登录,则退出应用程序。总之,在MainForm显示之前,要先显示SplachScreenForm和LoginForm。任何情况下,只有MainForm初始化完成并且登录成功,用户才能看到MainForm。

Program类

    static class Program
    {
        delegate DialogResult ShowLoginCallBack();
        delegate void CloseFormCallBack();
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            SplachScreenForm.ShowSplachScreen();    //首先显示SplachScreenForm
            if (SplachScreenForm.Init())   //如果界面初始化成功
            {
                DialogResult result = ShowLogin();     //调用SplachScreenForm中的ShowLogin方法,显示LoginForm
                if (result == DialogResult.OK)
                {
                    MainForm mainForm = SplachScreenForm.MainForm;
                    CloseForm();     //关闭SplachScreenForm
                    Application.Run(mainForm);    //启动主窗体
                }
            }

        }

        //线程间安全调用启动窗体的ShowLogin方法
        static DialogResult ShowLogin()
        {
            try
            {
                if (SplachScreenForm.SplachScreen.InvokeRequired)
                {
                    ShowLoginCallBack showLogin = new ShowLoginCallBack(ShowLogin);
                    return (DialogResult)SplachScreenForm.SplachScreen.Invoke(showLogin);
                }
                else
                {
                    return SplachScreenForm.ShowLogin();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                Common.CommonLib.ShowError(ex.Message);
                return DialogResult.Cancel;
            }
        }

        //线程间安全调用启动窗体的CloseForm方法
        static void CloseForm()
        {
            if (SplachScreenForm.SplachScreen.InvokeRequired)
            {
                CloseFormCallBack closeForm = new CloseFormCallBack(CloseForm);
                SplachScreenForm.SplachScreen.Invoke(closeForm);
            }
            else
            {
                SplachScreenForm.CloseForm();
            }
        }
    }

SplachScreenForm代码

     public partial class SplachScreenForm : Form
    {
        delegate DialogResult ShowDialogCallBack(LoginForm loginForm);
        static MainForm mainForm;
        private static SplachScreenForm splachScreen ;
        private static Thread thread;

        public SplachScreenForm()
        {
            InitializeComponent();
        }

        static SplachScreenForm()
        {
            mainForm = new MainForm();

        }

        public static SplachScreenForm SplachScreen
        {
            get { return splachScreen; }
        }

        public static MainForm MainForm
        {
            get { return mainForm; }
        }

        public static void ShowSplachScreen()
        {
            if (splachScreen != null)
                return;
            thread = new Thread(new ThreadStart(ShowForm));  //开启一个线程,显示SplachScreenForm
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        private static void ShowForm()
        {
            splachScreen = new SplachScreenForm();
            Application.Run(splachScreen);
        }

        public static bool Init()
        {
            return mainForm.Manager.SystemMenuForm.Init();  //初始化MainForm界面
        }

        public static DialogResult ShowLogin()
        {
            LoginForm loginForm = new LoginForm();
            loginForm.Owner = splachScreen;
           
            return loginForm.ShowDialog();   //显示LoginForm,登录成功返回DialogResult.OK
        }

        public static void CloseForm()
        {
            splachScreen.Close();
            mainForm = null;
            thread = null;
            splachScreen = null;
        }

    }

阅读更多
想对作者说点什么? 我来说一句

SplashScreen

2012年10月26日 111KB 下载

java简单登录窗体

2016年09月07日 1.07MB 下载

access登录窗体宏设计

2012年06月19日 588KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭