用户界面仍然使用backgroundWorker冻结

用户界面仍然使用backgroundWorker冻结

本文介绍了用户界面仍然使用backgroundWorker冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WPF很陌生.我正在开发PRISM应用程序,并希望在更新枚举时更新UI.我在modelView中使用backgroundWorker来更新枚举.一切正常,直到它自身的枚举被更新,然后UI冻结!一位朋友告诉我,我也许可以使用yield关键字,但我不太清楚.

I'm quite new to WPF. I am developing a PRISM application and want to update the UI when an enumerable is updated.I use a backgroundWorker in my modelView to update the enumaration. it all works fine until the enumeration it self gets updated and then the UI freezes!!a friend told my I might be able to use the yield keyword but I didn't quite figured it out.

这是代码:

public void ChangeCollection()
    {
        BackgroundWorker worker = new BackgroundWorker();

        // Set workers job
        worker.DoWork += (sender, e) =>
            {
                RunOnUIThread(() => IsBusy = true);
                e.Result = GetPrimes();
            };

        // On Complete
        worker.RunWorkerCompleted += (sender, e) =>
            {
                RunOnUIThread(() =>
                    {
                        IsBusy = false;

                        // HERE IS WHERE IT GETS STUCK
                        Numbers = new ObservableCollection<int>
                            ((IEnumerable<int>)e.Result);
                    });
            };

        // Start background operation
        worker.RunWorkerAsync();
    }

    public ObservableCollection<int> Numbers
    {
        get {return _Numbers;}
        set
        {
            _Numbers = value;
            RaisePropertyChanged(() => Numbers);
        }
    }

    public IEnumerable<int> GetPrimes()
    {
        List<int> primes = new List<int>();

        for (int i = 0; i < 100000; i++)
        {
            bool IsPrime = true;

            for (int j = 2; j < i; j++)
            {
                if (i % j == 0)
                    IsPrime = false;
            }

            if (IsPrime)
                primes.Add(i);
        }

        return primes;
    }

任何建议都将得到极大的应用!

Any advise would be greatly appriciated!

谢谢,欧姆里

推荐答案

这里有几件事.1)实例化包含它的对象时,应创建您的工作程序及其委托.

A few things here.1) Your worker and its delegates should be created when your object containing it is instantiated.

public class ViewModel
{
    BackgroundWorker _primeWorker;

    public ViewModel()
    {
        _primeWorker = new BackgroundWorker;

        _primeWorker.DoWork += ...
    }

    public void AddSomeNumbers()
    {
         if(_primerWorker.IsBusy == false)
              _primeWorker.RunWorkerAsync();
    }
}

2)当实例化包含它的对象时,应该实例化您的集合,以避免使用该类的对象调用get时引发空异常.

2) Your collection should be instantiated when the object containing it is instantiated to avoid a null exception being thrown should the object using this class calls the get.

3)由于每次添加一个数字,添加许多项会导致速度变慢.会触发UI线程必须处理的事件.

3) Adding that many items would cause slowness due to each time you add a number an event is fired that the UI thread has to handle.

此链接提供了更多信息来帮助您. http://msdn.microsoft.com/en -us/library/cc221403(v = vs.95).aspx

This link has more info to help you out. http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

这篇关于用户界面仍然使用backgroundWorker冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:39