winform多线程方式登录代码整理

bool restart = true;
while (restart)
{
      Start(out restart);
}

(1):在主进程中不停的调用start.

void Start(out bool restart)
{
            bool connected = false;
            restart = false;
            string[] userInfo = null;
            try
            {
                //打开LoginInfoForm窗体,返回用户名密码。
                userInfo = GetLoginInfo();// Splasher.GetLoginInfo();
                if (userInfo == null)
                {
                    loginOK = false;
                    return;
                }
                //启动登陆后台线程。LoginProcessForm窗体启动MySplashThread启动不停的打开LoginProcessForm窗体。
                Splasher.Show();
            }
            catch (Exception err)
            {
                DebugHelper.Write(err);
                Splasher.ShowDialog("录入登录信息发生异常,请和管理员联系", MessageTipType.Error);
                return;
            }
            eProcessState previousStepResult = eProcessState.Complete;
            while (Splasher.StartExecute())
            {
                try
                {
                    switch (Splasher.CurrentStep)
                    {
                        case eLoginState.__:
                        case eLoginState.__:
                        case eLoginState.__:
                        case eLoginState.__:
                    }
                    previousStepResult = eProcessState.Complete;

                    //结束执行
                    Splasher.EndExecute(previousStepResult);
                }
                catch (Exception error)
                {

                  //略
                 }
            }
        }

       //start中的对过程的控制。

static public void Show()
        {
            try
            {

                //当第二次调用的时候MySplashThread != null了这个时候
                if (MySplashThread != null)
                {

                    // public void Reset()
                    //{
                    //  progressView.Reset();
                    //currentItemIndex = 0;
                    //}

                    //相当于触发MySplashForm.Reset事件。

                    MySplashForm.Invoke(new MethodInvoker(MySplashForm.Reset));

                    //将窗体显示出来。
                    MySplashForm.Invoke(new MethodInvoker(MySplashForm.Show));

                    return;
                }

               //当第一次的时候调用构造函数构造出窗体。构造出来以后myResetEvent.Set();通知
                if (MySplashForm == null)
                {
                    MySplashForm = new LoginProcessForm(myResetEvent);
                }       

               //这里只是每次都调用Application.Run(MySplashForm);作用相当于建立起信息循环队列
                MySplashThread = new Thread(new ThreadStart(Splasher.ShowThread));
                MySplashThread.IsBackground = true;
                MySplashThread.SetApartmentState(ApartmentState.STA);
                MySplashThread.Start();

                //等待直到myResetEvent.Set();
                myResetEvent.WaitOne(5000);
            }
            catch (Exception err)
            {
                DebugHelper.Write(err);
            }
        }

//构造函数。

public LoginProcessForm(AutoResetEvent autoreset)
        {
            myResetEvent = autoreset;

            Renderer = new SkinFormLoginFormRender();
            InitializeComponent();
            //this.TopMost = true;
            //this.CloseBox = true;
            CanMoveWindow = false;
            labelVersion.Text = "Version:" + AssemblyInfoHelper.AssemblyFileVersion;

            this.Load += new EventHandler(LoginProcessForm_Load);

        }

delegate void EndExecuteCallback(eProcessState previousProcessResult);
        static public void EndExecute(eProcessState previousProcessResult)
        {

           //winform中子线程不能更新主线程的控件。所以MySplashForm.InvokeRequired代表是否需要更新主线程控件。

          //如果需要更新的话MySplashForm.Invoke
            if (MySplashForm.InvokeRequired)
            {

               //回调会继续执行previousProcessResult,等下次的时候就会到下面去执行了。
                MySplashForm.Invoke(new EndExecuteCallback(EndExecute), previousProcessResult);
            }
            else
            {

                 //如果
                MySplashForm.EndExecute(previousProcessResult);
            }
        }

 /// <summary>
        /// 进入下一过程
        /// </summary>
        /// <param name="previousProcessResult"></param>
        /// <returns></returns>
        public void EndExecute(eProcessState previousProcessResult)
        {

            //progressView为我们使用的用户控件
            progressView.SetTip(processItems[currentItemIndex], previousProcessResult);
            currentItemIndex++;
        }

//关闭代码

static public void Close()
        {
            try
            {
                if (MySplashThread == null) return;
                if (MySplashForm == null) return;
                try
                {
                    MySplashForm.Invoke(new MethodInvoker(MySplashForm.Close));
                }
                catch (Exception err)
                {
                    DebugHelper.Write(err);
                }
                MySplashThread.Abort();
            }
            catch (Exception err)
            {
                DebugHelper.Write(err);
            }
            finally
            {
                MySplashThread = null;
                MySplashForm = null;
            }
        }

原文地址:https://www.cnblogs.com/lzjsky/p/2106554.html