基本信息
源码名称:c# 导入Excel 委托 进度条 可复用
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2018-08-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
导入excel时候由于时间过长 窗体会假死,利用 委托线程 展示进度条 给用户良好体验


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 WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        //声明委托
        public delegate void SetPos(string showLabelText);
        //存储委托的事件
        SetPos sp;
        //存储当前Label 的显示文本
        string show;
        /// <summary>
        /// 初始化窗体的时候给 委托绑定 函数
        /// </summary>
        /// <param name="showLabelText">Label显示的值</param>
        /// <param name="sp">函数</param>
        public Form2(string showLabelText, SetPos sp)
        {
            InitializeComponent();
            this.sp = sp;
            show = showLabelText;
        }
        //声明一个线程
        Thread td;
        
        /// <summary>
        /// 当调用show() 或者 ShowDialog() 函数时 创建线程 执行委托的 函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Load(object sender, EventArgs e)
        {
            //给当前窗体Label 赋值
            this.label1.Text = show   "请勿关闭!";
            //让匿名线程执行委托的方法
            td = new Thread(delegate() { sp(show); });
            //标记为后台线程
            td.IsBackground = true;
            //开始执行
            td.Start();
            //设定 进度条的最大值是100
            this.backgroundWorker1.RunWorkerAsync(100);
        }
    
        //操作开始时用另一个线程执行
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = this.RetrieveData(this.backgroundWorker1, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }

        }
        //计算滚动条 速度
        private int RetrieveData(BackgroundWorker backgroundWorker, DoWorkEventArgs e)
        {
            int SumRecords = (int)e.Argument;
            int pecent = 0;
            for (int i = 0; i <= SumRecords; i  )
            {
                //每循环一次判断一次执行委托方法的线程是否结束 
                if (!td.IsAlive)
                {
                    //如果结束(提前结束) 将进度条显示调整到满格
                    backgroundWorker.ReportProgress(100);
                    return 100;
                }
                
                if (backgroundWorker.CancellationPending)
                    return i;
                pecent = (int)((double)i / (double)SumRecords * 100);
                backgroundWorker.ReportProgress(pecent);//执行状态
                //如果 执行委托 方法的线程还没结束  进度条满格后重新 执行  
                if (i == 100)
                {
                    i = 1;
                    //打断线程 为了清楚显示满格状态
                    Thread.Sleep(600);
                }
                //每间隔100毫秒执行一次该方法
                Thread.Sleep(100);

            }
            return SumRecords;
        }
        //辅助线程接收到 变动时 改变 进度条长度
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //显示百分比
            //this.label2.Text = e.ProgressPercentage   "%";
            //让进度条动起来
            this.progressBar1.Value = e.ProgressPercentage;
        }
        //无论成功还是失败都会执行该事件
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                MessageBox.Show("上传成功");
                this.Close();
            }
            catch
            {
                MessageBox.Show(e.Result.ToString());
            }
        }


    }

}