【问题标题】:Adding a progress bar to indicate that the file is done processing添加进度条以指示文件已完成处理
【发布时间】:2012-08-26 16:39:33
【问题描述】:

我是 C# 编程的新手,我很难在我的项目文件上放置进度条。你能帮我解决这个问题吗?

下面是我的代码:

try
{
    lblUpdate.Visible = true;
    lblUpdate.Refresh();

    string[] filenames = Directory.GetFiles(sTargetFolderPath);

    // Zip up the files - From SharpZipLib Demo Code
    using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
    {
        s.SetLevel(9); // 0-9, 9 being the highest level of compression

        byte[] buffer = new byte[4096];

        foreach (string file in filenames)
        {

            ZipEntry entry = new ZipEntry(Path.GetFileName(file));

            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);

            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    s.Write(buffer, 0, sourceBytes);

                } while (sourceBytes > 0);
            }
        }
        s.Finish();
        s.Close();
    }
}

【问题讨论】:

标签: c# .net progress-bar


【解决方案1】:

您必须使用后台工作程序才能在处理文件时不冻结 UI 在 DoWork 方法中设置您的流程 并使用 Progress 事件来设置你的进度条 这里有一篇文章解释了如何 http://www.dotnetperls.com/progressbar 将您的进度条最大属性设置为 filenames.Length 的文件数

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Shown += new EventHandler(Form1_Shown);

            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;
            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }
        void Form1_Shown(object sender, EventArgs e)
        {
            // Start the background worker
            backgroundWorker1.RunWorkerAsync();
        }
        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           int i = 0; 
            foreach (string file in filenames)    
            {
                i++;
                // Report progress to 'UI' thread
                backgroundWorker1.ReportProgress(i);
               // Your background task goes here zip files
            }
        }
        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }
    }

【讨论】:

    【解决方案2】:

    从工具书中制作一个进度条对象(只需拖放{或者您可以通过任何您想要的代码自己制作一个})

    然后取你运行的文件数,(100 / nNumOfFiles) 并按该数量进行进度条循环的每次迭代这里是编译后的代码,其中“prbProgressBar”是进度条

        try
    {
    
    lblUpdate.Visible = true;
    lblUpdate.Refresh();
    string[] filenames = Directory.GetFiles(sTargetFolderPath);
    
    // Zip up the files - From SharpZipLib Demo Code
    using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
    {
        s.SetLevel(9); // 0-9, 9 being the highest level of compression
    
        byte[] buffer = new byte[4096];
        int nPercentToAdvance = (100 / filenames.Length);
        foreach (string file in filenames)
        {
    
            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
    
            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);
    
            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    s.Write(buffer, 0, sourceBytes);
    
                } while (sourceBytes > 0);
            }
    
            this.prbProgressBar.Value += nPercentToAdvance;
        }
        s.Finish();
        s.Close();
    }
    

    }

    【讨论】:

    • -1 你测试过这个吗? UI 不会在后面的代码运行之前绘制,并且 prbProgressBar.Value 只会显示最终值。
    • 进度条仍然没有开始处理。我觉得有什么不足。但我不知道是哪一部分。
    • @Botsokoy91 你喜欢阅读我的评论吗?这是对为什么它没有进展的解释。看到哈桑的正确答案了吗?
    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2019-01-28
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多