在WPF实时制作进度条更新 [英] Making a progress bar update in real time in wpf

查看:2621
本文介绍了在WPF实时制作进度条更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦让进度条显示的实时更新。

I'm having some trouble making the progress bar show the updates in real time.

这是我的代码,现在

for (int i = 0; i < 100; i++)
{
     progressbar1.Value = i;
     Thread.Sleep(100);
}



但由于某些原因,进度条显示为空的功能运行时,再什么,直到该函数完成运行。可以给我一个人解释如何可以做到这一点?我是新的C#/ WPF所以我不是100%肯定的我将如何实现在不同的线程(如被看见一些其他职位)来解决这个问题,一个调度程序。

But for some reason the progress bar shows empty when the function runs, and then nothing until the function finishes running. Can someone explain to me how this can be done? I'm new to C#/WPF so I'm not 100% sure on how I would implement a Dispatcher on a different thread (as seen on some other posts) to fix this problem.

要澄清,我的程序有一个按钮,按下时,从一个文本框抓住价值,以及使用API​​来获取信息,并创建一个基于它的标签。我想每一个数据行被处理完毕后,进度条进行更新

To clarify, my program has a button which when press, grabs the value from a textbox, and uses an API to retrieve info, and create labels based on it. I want the progress bar to update after every row of data is finished processing.

这就是我现在所拥有的:

This is what I have right now:

private async void search(object sender, RoutedEventArgs e)
{
    var progress = new Progress<int>(value => progressbar1.Value = value);
    await Task.Run(() =>
    {
        this.Dispatcher.Invoke((Action)(() =>
        {
             some pre-processing before the actual for loop occur
             for (int i = 0; i < numberofRows; i++)
             {
                  label creation + adding
                  ((IProgress<int>)progress).Report(i);
             }
        }));
    });
}

感谢您!

推荐答案

管理,使其工作。所有我需要做的是而不是使它只是

Managed to make it work. All I needed to do is instead of making it just

progressBar1.value = i;



我不得不这样做。

I just had to do

progressbar1.Dispatcher.Invoke(() => progressbar1.Value = i, DispatcherPriority.Background);

这篇关于在WPF实时制作进度条更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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