在WPF使用BackgroundWorker的进度与 [英] Using BackgroundWorker with ProgressBar in WPF

查看:109
本文介绍了在WPF使用BackgroundWorker的进度与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个应用程序,其中的工作之一将是转换一个Excel,并通过所有记录到数据库。

Hi i have an App where one of the jobs will be convert an Excel and pass all the records to the DB.

因此​​,这需要一点时间,因为它是7000多行,我会和插入到数据库中。

So this takes a little time because it's more than 7000 rows that i will get and insert into the DB.

所以,因为这项工作需要一点时间,超过3分钟,我想用一个进度报告这项工作的进展情况。

So because this job takes a little time, more than 3 minutes, I'd like to use a ProgressBar to report progress of this job.

所以,如果我这样做在类这个工作,我已经创建了,我怎么能使用BackgroundWorker的报告进展情况向progessBar在我的情况?

So if i am doing this Job in Class that i have created, how can i use the backgroundWorker to report the progress to the progessBar in my case?

我的目标是要在进度是怎么回事的比例准确,我怎么可以用这些东西来报告进展情况。我从来没有与backgroundWorkers工作。

My objective is be accurate in the percentage of how the progress is going, and how i can use all this stuff to report the progress. I have never worked with backgroundWorkers.

我想,这只是一个提示,也许好还是不好,我第一次得到了Excel中的行数,使这个数字因为一些MAXVALUE的进度,然后为每行或在间隔我汇报进展情况。

I think, and this is only a tip, maybe good or not, that i first get the number of rows in the excel, make that number as some Maxvalue in the ProgressBar, and then for each Row or in an interval i report the progress.

这是可能的吗?我该怎么办呢?

This is possible? How can i do it?

在此先感谢!

推荐答案

我喜欢用ProgressBars绑定(和几乎其他一切可能的话),因为这样你就不需要担心的调度到UI线程

I like to use bindings for ProgressBars (and pretty much everything else where possible) because that way you do not need to worry about dispatching to the UI thread.

基本上你创建一些类,它实现的 INotifyPropertyChanged的,提供可以绑定你的进度,以进度属性。 。例如

Basically you create some class that implements INotifyPropertyChanged with a progress property that you can bind your ProgressBar to. e.g.

public class Task : INotifyPropertyChanged
{
    private int _progress = 0;
    public int Progress
    {
        get { return _progress; }
        private set
        {
            if (_progress != value)
            {
                _progress = value;
                OnPropertyChanged("Progress");
            }
        }
    }

    public Task(ref ProgressChangedEventHandler progressChangedEvent)
    {
        progressChangedEvent += (s, e) => Progress = e.ProgressPercentage;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

本类使用的事件构造函数,如果提出,但你可以处理你喜欢的任何方式改变的进展,这将更新任务的进度,例如:方法或通过使进度财产公共,所以你可以改变它随意。

This class uses an event in the constructor which will update the Task's progress if raised but you can handle the Progress change in any way you like, e.g. a method or by making the Progress property public so you can just change it arbitrarily.

用法示例:

<ProgressBar Minimum="0" Maximum="100" Height="20"
             Value="{Binding UpdateTask.Progress, Mode=OneWay}"/>





// The event that should be raised when a progress occurs.
private event ProgressChangedEventHandler UpdateProgressChanged;

// The task the ProgressBar binds to.
private readonly Task _updateTask;
public Task UpdateTask
{
    get { return _updateTask; }
}

public MainWindow()
{
    // Instatiate task, hooking it up to the update event.
    _updateTask = new Task(ref UpdateProgressChanged);
}

private void OnUpdateProgressChanged(int progressPercentage)
{
    if (UpdateProgressChanged != null)
    {
        UpdateProgressChanged(this, new ProgressChangedEventArgs(progressPercentage, null));
    }
}

// Simple progress simulation
private void Button1_Click(object sender, RoutedEventArgs e)
{
    int progress = 0;
    DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0.5) };
    timer.Tick += (sSub,eSub) =>
    {
        progress++;
        // Raise progress changed event which in turn will change
        // the progress of the task which in turn will cause
        // the binding to update which in turn causes the value
        // of the ProgressBar to change.
        OnUpdateProgressChanged(progress);
        if (progress == 100)
        {
            timer.Stop();
        }
    };
    timer.Start();
}

这篇关于在WPF使用BackgroundWorker的进度与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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