的WinForms进度条不更新(C#) [英] Winforms Progress bar Does Not Update (C#)

查看:148
本文介绍了的WinForms进度条不更新(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序[C#+ WinForms的。我有进度条和放大器;列表显示。

In my program [C# + winforms]. I have progress bar & listview.

通过一个方法,我在执行某些操作和放大器;然后在列表视图更新数据。在没有添加的记录就是我设置ProgressBar.value属性的值。我想在这里,根据进度条的值,它应该显示其进度。然而,进度条没有得到更新。只有在方法执行进度栏的末尾显示整个进度,即100%

Through one method i am performing some operations & then updating data in Listview. The no of records added is the value i am setting for ProgressBar.value property. What i want here is, According to value of progress bar, it should show its progress. However the progress bar is not getting updated. Only at the end of method execution progress bar shows entire progress i.e. 100 %

有人可以帮助我在这方面?

Can someone help me in this regard?

谢谢,
阿米特

Thanks, Amit

推荐答案

这听起来像你正在阻塞UI线程 - 即你没有发布系统做任何绘画

It sounds like you are blocking the UI thread - i.e. you haven't released the system to do any painting.

一个哈克的答案是注入 Application.DoEvents()到code - 但这是有风险的,与重入等问题;而且它只是一个有点哈克。

A hacky answer is to inject Application.DoEvents() into your code - but this is risky, and has problems with re-entrancy etc; and it is just a bit hacky.

有一个更好的选择可能是做一个的BackgroundWorker 的处理,周期性地切换到UI线程更新的东西(Control.Invoke) - 但是这可能会非常棘手,如果要添加大量的产品来让的ListView

A better option may be to do the processing on a BackgroundWorker, periodically switching to the UI thread to update things (Control.Invoke) - but this may be tricky if you are adding lots of items to a ListView.

完整的示例(尽管你可能要批UI更新 - 不是排在一个时间):

Full example (although you might want to batch the UI updates - not a row at a time):

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

class MyForm : Form
{
    BackgroundWorker worker;
    ListView list;
    Button btn;
    ProgressBar bar;
    public MyForm()
    {
        Text = "Loader";
        worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        list = new ListView();
        list.Dock = DockStyle.Fill;
        Controls.Add(list);
        btn = new Button();
        btn.Text = "Load";
        btn.Dock = DockStyle.Bottom;
        Controls.Add(btn);
        btn.Click += btn_Click;
        bar = new ProgressBar();
        bar.Dock = DockStyle.Top;
        Controls.Add(bar);
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        btn.Enabled = true;
    }

    void btn_Click(object sender, EventArgs e)
    {
        worker.RunWorkerAsync();
        btn.Enabled = false;
    }


    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            string newRow = "Row " + i.ToString();
            worker.ReportProgress(i, newRow);
            Thread.Sleep(100);
        }
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        list.Items.Add((string)e.UserState);
        bar.Value = e.ProgressPercentage;
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MyForm());
    }
}

这篇关于的WinForms进度条不更新(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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