C# 为什么我的进度条不能反映状态

C# 为什么我的进度条不能反映状态,c#,multithreading,progress-bar,C#,Multithreading,Progress Bar,我根据我正在处理的另一个项目设置了以下测试,似乎无法让进度条在复制文件时显示状态 BackgroundWorker workerThread = null; public Form2() { InitializeComponent(); InstantiateWorkerThread(); } private void InstantiateWorkerThread() { workerThread = new BackgroundWorker(); wor

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

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);
    }
}
BackgroundWorker workerThread=null;
公共表格2()
{
初始化组件();
实例化wrkerthread();
}
私有void实例化wrkerthread()
{
workerThread=新的BackgroundWorker();
workerThread.ProgressChanged+=workerThread\u ProgressChanged;
workerThread.DoWork+=workerThread\u DoWork;
workerThread.WorkerReportsProgress=true;
workerThread.workerSupportsScanCellation=true;
}
私有void WorkerThread_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
lblStopWatch.Text=(“进度:+e.ProgressPercentage.ToString()+“%”);
progressBar1.值=e.ProgressPercentage;
}
私有void WorkerThread_DoWork(对象发送方,DoWorkEventArgs e)
{

对于(int i=0;i要报告进度,您必须知道总共要复制多少个文件,以及到目前为止已复制了多少个文件。为此,您必须首先将从
目录返回的路径存储到一个变量中。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);
}
private void WorkerThread\u DoWork(对象发送方,DoWorkEventArgs e)
{
常量字符串pathFrom=@“C:\Test\WA8\CLR”;
常量字符串路径=@“C:\Test\Test”;
string[]filepath=Directory.GetFiles(pathFrom);
for(int i=0;i
我当前版本的Theodors示例现在可以使用了

string[] filestoCopy = Directory.GetFiles(pathFrom);

for (int i = 0; i <= filestoCopy.Length; i++)
{
    int u = (i * 100 / filestoCopy.Length);

    // Report progress to 'UI' thread
    workerThread.ReportProgress(u);
    // Simulate long task
    File.Copy(filestoCopy[i], Path.Combine(pathTo, Path.GetFileName(filestoCopy[i])), true);
}
workerThread.ReportProgress(100);
string[]filestoCopy=Directory.GetFiles(pathFrom);

对于(int i=0;我执行lblStopWatch.Text更新?除非我在
copytest()
中抛出异常,否则我无法重现该问题,然后调试器当然会告诉我。是否附加了调试器?是否在设计器中更改了
progressBar1
的任何属性?您发布的代码工作正常(当然,如前一条注释所述,假设
copytest()
方法毫无例外地完成)。如果您有问题,则代码必须不同。您需要包含一个可靠地再现问题的好代码。请注意,这可能需要包含通常在
*.Designer.cs
文件中完成的所有初始化,以防在设计器中配置程序时出错。我已在代码中发布了所有内容文本不会更新,进度条也不会更新。没有异常发生,源文件夹中有59个文件,有些文件大小为1gb,正如我所说的,传输大约需要30到45秒。运行代码后,所有59个文件都在目标文件夹中,因此复制工作正常。唯一不起作用的是文本和进度条..:(我验证了,在原始代码中唯一没有发布的是这一行,我已经添加到BackgroundWorker workerThread=null上面的代码中;除此之外,整个Form2.cs代码都在上面,就像我说的,这是在我尝试之前和在我的生产应用程序上进行的一个简单的概念测试..谢谢你,当你发布你的代码时,我正要发布类似的内容,但我的不起作用..你的和我的不同之处在于我使用的是filepath.Count(),而我的进度数学是反向的。我更改了数学行的代码并添加了ReportProgress(100),现在似乎可以了。