后台更新进度条的特定用例

本文关键字:更新 后台 | 更新日期: 2023-09-27 18:00:11

我已经为这个问题寻找了两天多的解决方案,最终决定问这个问题。我找到了很多相关的话题,但似乎没有一个能解决我的问题。最近,我尝试了这里列出的所有解决方案。

背景信息:我有一个处理遍历大量数据的类。该类名为Traverse。有一个名为DoFullTraverse(Traverse.DoFullTra弗斯)的类方法,它运行一个完整的遍历,可能需要长达30秒的时间(取决于用户输入)。我在WPF,MVVM模式下工作。我想为DoFullTraverse的进度更新gui上的状态栏。我在函数的开头计算出计算所需的确切循环数,然后递增循环计数器。每当它达到另一个1/100时,我会将进度条增加1。我的进度条(在xaml中)的值绑定到VM中名为PBarV的属性。

最近的尝试:我尝试了100种不同的解决方案,但我最近的尝试看起来是这样的:

private void runTraverseAndUpdateBar()
{
    var worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Complete);
    worker.RunWorkerAsync();
    while (!ThreadCheck)
    {
        Thread.Sleep(500);
    }
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
    var worker = sender as BackgroundWorker;
    for (int i = 0; i < 36; i++)
    {
        Thread.Sleep(500);
        PBarV += 3;
    }
    e.Result = true;
}
void worker_Complete(object sender, RunWorkerCompletedEventArgs e)
{
    ThreadCheck = true;
}

我认为我从根本上误解了背景工作者的工作方式。

主要问题:如果我把函数放入后台工作程序并像往常一样继续,我可以让这个方法正常工作。问题是,在我的程序继续之前,我需要该函数的数据。因此,我需要它线性地执行,但仍然正确地更新状态栏。

如果有人能阐明我所缺少的东西,甚至推动我朝着正确的方向前进,我将不胜感激。

编辑:这不是重复的。您提供的帖子不涉及线性执行和等待后台工作人员完成后再继续的问题

编辑2:(根据@Clemens请求)

我需要后台工作人员在主程序继续之前完成工作。我在后台工作人员中运行计算量很大的进程,以便可以更新进度条。但是,在主程序可以继续之前,我需要Traverse.DoFullTraverse(); 的信息

非常具体地说。主程序应停止所有执行(除了更新状态栏),直到后台工作人员完成Traverse.DoFullTraverse();

后台更新进度条的特定用例

这里有一个简单的例子,您可以使用它并将其应用于视图模型。使用原型创建代码并了解其工作原理,以便将其应用于更大、更复杂的应用程序,这一点非常重要。

请注意,该示例不包括诸如如何实现INotifyPropertyChanged和ICommand之类的琐碎内容——这些都很容易做到

此外,请注意TraverseYo中的注释。特别是那些告诉你当前在哪个线程上的线程。了解跨线程执行的流程对于正确工作很重要。如果你不知道你在哪个线程上,只需获取当前线程的ApartmentState。如果是STA,那么您很可能在UI线程上。

public class LongLastingWorkViewModel : INotifyPropertyChanged
{
    public bool Busy 
    {
        // INotifyPropertyChanged property implementation omitted
    }
    public double PercentComplete 
    {
        // INotifyPropertyChanged property implementation omitted
    }
    public ICommand PerformWork { get; set; }
    public LongLastingWorkViewModel()
    {
        // delegated ICommand implementation omitted--there's TONS of it out there
        PerformWork = new DelegatedCommand(TraverseYo);
    }
    private void TraverseYo()
    {
        // we are on the UI thread here
        Busy = true;
        PercentComplete = 0;
        Task.Run(() => {
            // we are on a background thread here
            // this is an example of long lasting work 
            for(int i = 0; i < 10; i++)
            {
                Thread.Sleep(10 * 1000); // each step takes 10 seconds
                // even though we are on a background thread, bindings
                // automatically marshal property updates to the UI thread
                // this is NOT TRUE for INotifyCollectionChanged updates!
                PercentDone += .1; 
            }
            Busy = false;            
        });
}

您可以将Busy绑定到一个覆盖层,该覆盖层在执行时阻止所有UI,将PercentComplete绑定到进度条,并将PerformWork绑定到按钮。