进度条--ProgressBar和BackgroundWorker

本文介绍了一种使用BackgroundWorker处理就餐打卡数据并插入数据库的方法。通过计算文本文件的行数来显示处理进度,利用BackgroundWorker的DoWork、ProgressChanged等事件实现异步操作与进度更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1) 需求:就餐打卡数据处理后,插入数据库中,用进度条显示过程

2) 思路:总进度为txt文本文件的行数(数据都是按照行写入),文本文件的大小

//BackgroundWorker对象有三个主要的事件:

//DoWork - 当BackgroundWorker对象的多线程操作被执行时触发。

//RunWokerCompleted - 当BackgroundWoker对象的多线程操作完成时触发。

//ProgressChanged - 当BackgroundWorker对象的多线程操作状态改变时触发。

//WorkerReportsProgress - 如果想让BackgroundWorker对象以异步的方式报告线程实时进度,必须将该属性的值设为true。

//WorkerSupportsCancellation 布尔类型,该值指示BackgroundWorker是否支持异步取消。

//BackgroundWorker对象的ReportProgress方法用于向主线程返回后台线程执行的实时进度。 

        //总后台线程
        private BackgroundWorker bgwInsertData = new BackgroundWorker();
        //求百分比
        long totalCount = 0;
        decimal percent = 0;
     
        //源--就餐打卡记录储存 目录
        private string sourcefileDirectory = Application.StartupPath + "\\CardData";
        private string destFileDirectory = Application.StartupPath + "\\CardDataBak";        

        public FrmReadText()
        {
            InitializeComponent();

            bgwInsertData.WorkerReportsProgress = true;
            bgwInsertData.WorkerSupportsCancellation = true;

            bgwInsertData.DoWork += new DoWorkEventHandler(bkWorker_DoWork);
            bgwInsertData.ProgressChanged += new ProgressChangedEventHandler(bkWorker_ProgressChanged);
            bgwInsertData.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bkWorker_RunWorkerCompleted);
        }

        private void btnReadText_Click(object sender, EventArgs e)
        {
            //1.调用bgwInsertData的RunWorkerAsync方法,用来引发DoWork事件
            bgwInsertData.RunWorkerAsync();
        }

        void bkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (e.Cancelled)
            {
                MessageBox.Show("取消操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //关闭该窗体
                this.Close();
            }
        }

        void bkWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lblResult.Text = "已获取数据" + e.ProgressPercentage.ToString() + "%";
        }

        void bkWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            //2.在DoWork中调用自定义函数,并将引发DoWork事件的sender传递出去
            insertData(worker);  
        }
View Code

3)  效果图:

 

转载于:https://www.cnblogs.com/senlinzhang/p/8472745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值