⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 多线程导入数据,代进度条,本程序是.net语言编写,可以参考其中的例子
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.jdTools;
using Transition.User;
using Transition.qClass;
using Transition.qClassPoint;
using Transition.Question;
using System.Threading;

namespace TrainsitionApp
{
    
    public partial class frmMain : Form
    {
        private int step = 1;
        /// <summary>
        /// Step=1的时候开始导入用户信息
        /// Step=2的时候开始导入试题分类信息
        /// Step=3的时候开始导入试题考点信息
        /// Step=4的时候开始导入试题信息
        /// Step=5的时候完成导入
        /// </summary>
        public int Step
        {
            get
            {
                return this.step;
            }
            set
            {
                this.step = value;
            }
        }
        /// <summary>
        /// 当前状态
        /// </summary>
        protected string FrmTitle
        {
            get {
                if (step == 1)
                    return "正在导入用户数据";
                else if (step == 2)
                    return "正在导入试题分类数据";
                else if (step == 3)
                    return "正在导入分类考点数据";
                else if (step == 4)
                    return "正在导入试题数据";
                else if (step == 5)
                    return "完成导入";
                else
                    return "正在准备数据";
            }
        }

        private string Msg = "总记录{0}条 当前第{1}条 完成{2}%";
        private int TotalCount = -1;
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            reset();
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbStat.Value = e.ProgressPercentage;
            lblMsg.Text = e.UserState.ToString();
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                lblMsg.Text = "操作取消";
            }
            else if (e.Error != null)
            {
                lblMsg.Text = e.Error.Message + ",当前进行第" + e.Result == null ? "0" : e.Result.ToString() + "条";
            }
            else
            {
                lblMsg.Text = "完成操作";
                btnNext.Enabled = true;
            }
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Step == 1)
            {
                TransitionUserBegin((BackgroundWorker)sender, e);
            }else if(Step==2)
            {
                TransitionqClassBegin((BackgroundWorker)sender, e);
            }
            else if (Step == 3)
            {
                TransitionqClassPointBegin((BackgroundWorker)sender, e);
            }
            else if (Step == 4)
            {
                TransitionQuestionBegin((BackgroundWorker)sender, e);
            }
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            btnBegin.Enabled = false;
            btnCancel.Enabled = true;
            if (!bgWorker.IsBusy)
            {
                bgWorker.RunWorkerAsync();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Step < 5)
            {
                if (MessageBox.Show("确认取消向导?", "系统提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    if (bgWorker.IsBusy)
                    {
                        bgWorker.CancelAsync();
                    }
                    Application.Exit();
                }
            }
            else
            {
                Application.Exit();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (Step < 5)
            {
                Step++;//进入下一步
                reset();
                if (!bgWorker.IsBusy)
                {
                    bgWorker.RunWorkerAsync();
                }
            }
        }

        private void reset()
        {
            lblState.Text = FrmTitle;
            TotalCount = -1;
            if (step == 5)
            {
                btnNext.Visible = false;
                btnBegin.Visible = false;
                btnCancel.Text = "完 成";
            }
            else
            {
                btnNext.Enabled = false;
            }
        }


        #region 执行数据导入操作
        /// <summary>
        /// 开始导入用户数据
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="e"></param>
        private void TransitionUserBegin(BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (TotalCount == -1)
            {
                worker.ReportProgress(0, "向导正在查询数据总数");
                TotalCount = 1000;// TransitionUser.GetOldTotalCount();
            }
            int count = 10;     //每次导入10个用户
            for (int i = 0; i < TotalCount + 10; i = i + count)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    double ii = i;
                    worker.ReportProgress((int)(ii / TotalCount * 100 > 100 ? 100 : ii / TotalCount * 100), string.Format(Msg, TotalCount, ii, (int)(ii / TotalCount * 100)));
                    //开始
                    //TransitionUser.InToNew(i, count);
                    Thread.Sleep(1);
                }
            }
        }
        /// <summary>
        /// 开始导入试题分类数据
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="e"></param>
        private void TransitionqClassBegin(BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (TotalCount == -1)
            {
                worker.ReportProgress(0, "向导正在查询数据总数");
                TotalCount = TransitionqClass.GetOldTotalCount();
            }
            int count = 10;     //每次导入10个用户
            for (int i = 0; i < TotalCount+10; i = i + count)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    double ii = i;
                    worker.ReportProgress((int)(ii / TotalCount * 100 > 100 ? 100 : ii / TotalCount * 100), string.Format(Msg, TotalCount, ii, (int)(ii / TotalCount * 100)));
                    //开始
                    //TransitionqClass.InToNew(i, count);
                    Thread.Sleep(1);
                }
            }
        }
        /// <summary>
        /// 开始导入试题分类考点数据
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="e"></param>
        private void TransitionqClassPointBegin(BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (TotalCount == -1)
            {
                worker.ReportProgress(0, "向导正在查询数据总数");
                TotalCount = TransitionqClassPoint.GetOldTotalCount(); // TransitionqClass.GetOldTotalCount();
            }
            int count = 10;     //每次导入10个用户
            for (int i = 0; i < TotalCount + 10; i = i + count)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    double ii = i;
                    worker.ReportProgress((int)(ii / TotalCount * 100 > 100 ? 100 : ii / TotalCount * 100), string.Format(Msg, TotalCount, ii, (int)(ii / TotalCount * 100)));
                    //开始
                    //TransitionqClassPoint.InToNew(i, count);
                    Thread.Sleep(1);
                }
            }
        }
        /// <summary>
        /// 开始导入试题数据
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="e"></param>
        private void TransitionQuestionBegin(BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (TotalCount == -1)
            {
                worker.ReportProgress(0, "向导正在查询数据总数");
                TotalCount =TransitionQuestion.GetOldTotalCount(); // TransitionqClass.GetOldTotalCount();
            }
            int count = 15;     //每次导入10个用户
            for (int i = 0; i < TotalCount + 10; i = i + count)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    double ii = i;
                    worker.ReportProgress((int)(ii / TotalCount * 100 > 100 ? 100 : ii / TotalCount * 100), string.Format(Msg, TotalCount, ii, (int)(ii / TotalCount * 100)));
                    //开始
                    //TransitionQuestion.InToNew(i, count);
                    Thread.Sleep(1);
                }
            }
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -