为什么我的进度条没有反映状态 [英] Why isnt my progress bar reflecting the status

查看:36
本文介绍了为什么我的进度条没有反映状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据我正在处理的另一个项目设置了以下测试,但似乎无法让进度条显示正在复制文件的状态

BackgroundWorker workerThread = null;公共 Form2(){初始化组件();InstantiateWorkerThread();}私有无效 InstantiateWorkerThread(){workerThread = new BackgroundWorker();workerThread.ProgressChanged += WorkerThread_ProgressChanged;workerThread.DoWork += WorkerThread_DoWork;workerThread.WorkerReportsProgress = true;workerThread.WorkerSupportsCancellation = true;}私有无效WorkerThread_ProgressChanged(对象发送者,ProgressChangedEventArgs e){lblStopWatch.Text = ("进度:" + e.ProgressPercentage.ToString() + "%");progressBar1.Value = e.ProgressPercentage;}私有无效WorkerThread_DoWork(对象发送者,DoWorkEventArgs e){for (int i = 0; i <= 100; i++){//向UI"线程报告进度workerThread.ReportProgress(i);//模拟长任务复制测试();}}private void btnStart_Click(object sender, EventArgs e){workerThread.RunWorkerAsync();}私有无效 copytest(){string pathFrom = @"C:\Test\WA8\CLR";string pathTo = @"C:\Test\Test";foreach(Directory.GetFiles(pathFrom) 中的字符串文件){//将当前文件复制到新路径.File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true);}}

我在网上找到的例子中使用了这个方法

我也用我的代码尝试过这个例子,但不起作用

另外,我想更正我之前在下面的陈述,标签文本确实显示了,应该显示的百分比没有......所以标签正在显示,如屏幕截图所示

有些有趣的事情,我之前截取了表单的屏幕截图,这意味着我必须运行应用程序才能在镜头中显示标签,因为我在发布时没有关闭应用程序,所以我在之后又回到了它我之前的帖子,发现事情更新了,但不正确.全部 59 个文件都复制过来了,但即使复制已经完成,进度条也只显示部分绿色,标签反映了 5%.为什么流程会运行、完成,而进度条只反映完成了 5%?

解决方案

要报告进度,您必须知道总共需要复制多少个文件,以及到目前为止已复制了多少个文件.为此,您必须首先将从 Directory.GetFiles 返回的路径存储到变量:

private void WorkerThread_DoWork(object sender, DoWorkEventArgs e){const string pathFrom = @"C:\Test\WA8\CLR";const string pathTo = @"C:\Test\Test";string[] filePaths = Directory.GetFiles(pathFrom);for (int i = 0; i < filePaths.Length; i++){int currentProgress = (i * 100)/filePaths.Length;workerThread.ReportProgress(currentProgress);var filePath = filePaths[i];var fileName = Path.GetFileName(filePath);var newFilePath = Path.Combine(pathTo, fileName);File.Copy(filePath, newFilePath, overwrite: true);}workerThread.ReportProgress(100);}

I setup the following test based on another project im working on and cant seem to get the progress bar to show the status as its copying the files

BackgroundWorker workerThread = null;

public Form2()
{
    InitializeComponent();

    InstantiateWorkerThread();
}

private void InstantiateWorkerThread()
{
    workerThread = new BackgroundWorker();
    workerThread.ProgressChanged += WorkerThread_ProgressChanged;
    workerThread.DoWork += WorkerThread_DoWork;
    workerThread.WorkerReportsProgress = true;
    workerThread.WorkerSupportsCancellation = true;
}

private void WorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    lblStopWatch.Text = ("Progress: " + e.ProgressPercentage.ToString() + "%");
    progressBar1.Value = e.ProgressPercentage;
}

private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i <= 100; i++)
    {
        // Report progress to 'UI' thread
        workerThread.ReportProgress(i);
        // Simulate long task
        copytest();
    }
}

private void btnStart_Click(object sender, EventArgs e)
{
    workerThread.RunWorkerAsync();
}

private void copytest()
{
    string pathFrom = @"C:\Test\WA8\CLR";
    string pathTo = @"C:\Test\Test";

    foreach (String file in Directory.GetFiles(pathFrom))
    {
        // Copy the current file to the new path. 
        File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true);
    }
}

I am using this how to for my example i found online

I also tried this example with my code and not working Second how to attempted

What am i doing wrong with this setup? The copy works and takes about 30 seconds because there is only 50 files..

First how to attempted

My form is simple, Button, Progressbar and Label

Also i guess to correct my earlier statement below, the Label text does show up, the percentage that should be shown does not.. So the label is being displayed as seen in the screen shot

So something interesting, i took a screen shot of the form earlier which meant i had to run the application to show the label in the shot, well since i didnt close the application while i was posting, i came back to it after my earlier post and found that things updated, but not correctly. All 59 files were copied over, but even though the copy had already completed, the the progress bar only showed partially green and the label reflected 5%. Why would the process run, complete and the progress bar only reflect 5% complete?

解决方案

To report the progress you must know how many files you have to copy in total, and how many files have been copied so far. To do this you must begin by storing the paths returned from Directory.GetFiles to a variable:

private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
    const string pathFrom = @"C:\Test\WA8\CLR";
    const string pathTo = @"C:\Test\Test";
    string[] filePaths = Directory.GetFiles(pathFrom);
    for (int i = 0; i < filePaths.Length; i++)
    {
        int currentProgress = (i * 100) / filePaths.Length;
        workerThread.ReportProgress(currentProgress);
        var filePath = filePaths[i];
        var fileName = Path.GetFileName(filePath);
        var newFilePath = Path.Combine(pathTo, fileName);
        File.Copy(filePath, newFilePath, overwrite: true);
    }
    workerThread.ReportProgress(100);
}

这篇关于为什么我的进度条没有反映状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆