各位,以下代码实现的是从Y文件夹复制bmp文件到YTest文件夹。
而复制的过程中会实时的通过progressbar展现,功能上没有什么不正确,但效果上有点小问题。
当文件数目过大时,progressBar1.PerformStep();会占用到UI主线程,导致看着有点卡,或者说有点像死了的感觉。
是不是通过多线程能解决此问题呢,希望各位大牛不吝赐教,感激不尽啊!
            progressBar1.Visible = true;
            string directorySource = Application.StartupPath + "\\Y";
            string directoryDest = Application.StartupPath + "\\YTest";
            string[] filePathSource = Directory.GetFiles(directorySource, "*.bmp");
            string[] filePathDest = Directory.GetFiles(directoryDest);            progressBar1.Minimum = 1;
            progressBar1.Maximum = filePathSource.Length;
            if (filePathDest.Length > 0)
            {
                for (int i = 0; i < filePathDest.Length; i++)
                {
                    File.Delete(filePathDest[i]);
                }
            }
            for (int i = 0; i < filePathSource.Length; i++)
            {
                File.Copy(filePathSource[i],
                    directoryDest + "\\" + Path.GetFileNameWithoutExtension(filePathSource[i]) + ".bmp");
                progressBar1.PerformStep();
                Console.WriteLine(i);
            }
            progressBar1.Visible = false;

解决方案 »

  1.   

    上面代码太难看了,换个:progressBar1.Visible = true;
                string directorySource = Application.StartupPath + "\\Y";
                string directoryDest = Application.StartupPath + "\\YTest";
                string[] filePathSource = Directory.GetFiles(directorySource,
                    "*.bmp");
                string[] filePathDest = Directory.GetFiles(directoryDest);            progressBar1.Minimum = 1;
                progressBar1.Maximum = filePathSource.Length;
                if (filePathDest.Length > 0)
                {
                    for (int i = 0; i < filePathDest.Length; i++)
                    {
                        File.Delete(filePathDest[i]);
                    }
                }
                for (int i = 0; i < filePathSource.Length; i++)
                {
                    File.Copy(filePathSource[i],
                        directoryDest + "\\" + Path.GetFileNameWithoutExtension(
                        filePathSource[i]) + ".bmp");
                    progressBar1.PerformStep();
                    Console.WriteLine(i);
                }
                progressBar1.Visible = false;
      

  2.   


       private void DoWithCommon()
            {
                WaitCallback waitCallBack = new WaitCallback(this.InvokeMethod);
                ThreadPool.QueueUserWorkItem(waitCallBack, "Knights Warrior");
            }
            private void InvokeMethod(object x)
            {
                this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
            }
            private void ChangeUIWithCommon(string name)
            {
                this.lblMessage.Text = name;
            }在线程中修改UI控件
      

  3.   

    this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
    关键
      

  4.   

    用backgroundwork控件。
    在progressindexchange事件中设置progressBar1的value
      

  5.   

    every day,I will get the score
      

  6.   


    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 WindowsFormsApplication9
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public delegate void InvokeMethodDelegate(string name);
            
            private void button1_Click(object sender, EventArgs e)
            {
                DoWithCommon();
            }        private void DoWithCommon()
            {
                WaitCallback waitCallBack = new WaitCallback(this.InvokeMethod);
                ThreadPool.QueueUserWorkItem(waitCallBack, "Knights Warrior");
            }
            private void InvokeMethod(object x)
            {
                this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
            }
            private void ChangeUIWithCommon(string name)
            {
                this.button1.Text = name;
            }    }
    }
      

  7.   


     private void InvokeMethod(object x)
            {
                for (int i = 0; i < filePathSource.Length; i++)
                {
                    File.Copy(filePathSource[i],
                        directoryDest + "\\" + Path.GetFileNameWithoutExtension(
                        filePathSource[i]) + ".bmp");
                                   Console.WriteLine(i);
                       this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon));            }                   }
            private void ChangeUIWithCommon()
            {
                 progressBar1.PerformStep();
            }