小小线程记【转】

//1 全局定义
        int m_fileName;
        Thread m_thread;
        FileInfo[] filelist;
        // 定义进度条操作的委托函数原形
        public delegate void MyOption(int value);

        public Form6()
        {
            InitializeComponent();
        }


        //3 定义线程处理函数
        private void myProc()
        {
            for (int n = 0; n < filelist.Length; n++)
            {
                //Thread.Sleep(5000);
                string dd = @"D:\j\" + filelist[n];
                filelist[n].CopyTo(dd);
                this.Invoke(new MyOption(myOption), n);
            }
            ////处理Excel循环
            //for (int i = 0; i < m_fileName; i++)
            //{
            //    //执行SQL操作
            //    // 在线程中操作窗体控件,必须使用窗体的Invoke函数来执行,否则会有异常。
            //    this.Invoke(new MyOption(myOption), i);
            //}
        }

        //4 进度条处理
        private void myOption(int value)
        {
            this.progressBar1.Value = value;
            value += 1;
            label1.Text = value.ToString() + "/" + filelist.Length;
            if (value == this.progressBar1.Maximum)
            {
                MessageBox.Show("复制完成");
                this.progressBar1.Visible = false;
            }
        }

        private void Form6_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f1 = new FolderBrowserDialog();
            if (f1.ShowDialog() == DialogResult.OK)
            {
                DirectoryInfo di = new DirectoryInfo(f1.SelectedPath);
                filelist = di.GetFiles("*.jpg");

                //2 处理按钮事件触发函数
                m_fileName = filelist.Length;
                m_thread = new Thread(new ThreadStart(myProc));
                m_thread.Start();
                this.progressBar1.Maximum = m_fileName;
                this.progressBar1.Minimum = 0;
                this.progressBar1.Value = 0;
            }
        }

转载于:https://www.cnblogs.com/JemBai/archive/2008/08/12/1266183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值