Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为找到的每个项目更新progressbar_C#_Loops_For Loop_Foreach - Fatal编程技术网

C# 为找到的每个项目更新progressbar

C# 为找到的每个项目更新progressbar,c#,loops,for-loop,foreach,C#,Loops,For Loop,Foreach,我的progressbar和循环(在C#上)有问题。 我需要解析一组项目(通过foreach循环),对于找到的每个对象,我需要增加一个进度条(通过for循环)。。。但我的代码为每个找到的对象运行,这是正常的,但我找不到解决方法 以下是我的简化代码: int totalSteps = lv_selection.Items.Count; foreach (string p in lv_selection.Items) { for (int i

我的progressbar和循环(在C#上)有问题。 我需要解析一组项目(通过foreach循环),对于找到的每个对象,我需要增加一个进度条(通过for循环)。。。但我的代码为每个找到的对象运行,这是正常的,但我找不到解决方法

以下是我的简化代码:

int totalSteps = lv_selection.Items.Count;

        foreach (string p in lv_selection.Items)
        {
            for (int i = 0; i < totalSteps; i++)
            {
                // A time consuming job
                (sender as BackgroundWorker).ReportProgress((int)(100 / totalSteps) * i, null);

                // Update the progressbar's text (located in another form)
                this.Dispatcher.Invoke(() =>
                {
                    progressbarForm.Progress(p);
                });
            }
        }
int totalSteps=lv_selection.Items.Count;
foreach(lv_selection.Items中的字符串p)
{
对于(int i=0;i
{
进展(p);
});
}
}
progressbar的进度与预期一致,但对于集合中的每个项目(“p”变量)。我知道为什么,但我不知道如何解决这个问题

还尝试交换for和foreach循环。并在“耗时作业”后设置for循环

有人能帮我吗

非常感谢。

我终于拿到了!:)

我试着使用一个技巧,效果很好

我在foreach之前创建一个整数变量,设置为0。在foreach中,我将这个变量增加1,并将他的值作为一个步骤发送到progressbar

    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {

        int totalSteps = lv_selection.Items.Count;
        int currentStep = 0;

        // Installations
        foreach (string p in lv_selection.Items)
        {
            currentStep++;

            // My long task 

            this.Dispatcher.Invoke(() =>
            {
                progressbarForm.Progress(p);
            });

            (sender as BackgroundWorker).ReportProgress((int)(100 / totalSteps) * currentStep, null);

            }
    }

如果“progressbar按预期进度”,那么您的问题是什么?我不明白你在问什么“进度条”到底是什么?这是什么技术?MVC,WPF,Winforms,Xamarin??!抱歉,我可能解释错了:)我的意思是进度条填充正确,但找到的每个项目都会重新开始。如果我的收藏中有4项,progressbar将填充4次。它是一个WPF ProgressBar,用c代码管理。