GUI冻结而Dispatcher.BeginInvoke或Task.StartNew(GUI fro

2019-10-16 16:57发布

注:取决于这个quetion

你好。 我有一个视图模型是这样的:

public class ViewModel {
    private readonly IPersonService _personService;
    private readonly ObservableCollection<SearchPersonModel> _foundedList;
    private readonly DispatcherTimer _timer;
    private readonly Dispatcher _dispatcher;
    private CancellationTokenSource _tokenSource;

    public SearchPatientViewModel(IPersonService personService) {
        _personService = personService;
        _foundedList = new ObservableCollection<SearchPersonModel>();
        _dispatcher = (/*CurrentApplication*/).Dispatcher;
        _timer = new DispatcherTimer(
            TimeSpan.FromMilliseconds(1000),
            DispatcherPriority.Normal,
            TimerCallBack,
            _dispatcher);
        _tokenSource = new CancellationTokenSource();
    }

    public string Term {
        get { return _term; }
        set {
            // implementing INotifyPropertyChanged
            if(_term== value)
                return;
            _term= value;
            OnPropertyChanged(() => Term);
            tokenSource.Cancel(); // canceling prev search query
            _timer.Stop(); // stop the timer to reset it
            // start it again to do a search query if user change not the term for 1000ms
            _timer.Start(); 
        }
    }

    private void TimerCallBack(object sender, EventArgs e) {
        _timer.Stop();
        _tokenSource = new CancellationTokenSource();
        var task = Task<IEnumerable<SearchPersonModel>>.Factory
            .StartNew(Search, _tokenSource.Token);
        _dispatcher.BeginInvoke((Action)(() => {
            _foundedList.Clear();
            foreach(var item in task.Result)
                _foundedList.Add(item);
        }), DispatcherPriority.Background);
    }

    private IEnumerable<SearchPersonModel> Search() {
        return _personService.DoSearch(this.Term);
    }

}

而在IPersonService实现我这样做:

public class PersonService : IPersonService {
    public IEnumerable<SearchPersonModel> DoSearch(string term){
        System.Threading.Thread.Sleep(10000);
        return some-search-result;
    }
}

但是,我希望,虽然搜索执行查询时,GUI是免费的。 但它冻结! 你有任何想法,是我的错? 你能帮我吗? 提前致谢!

Answer 1:

问题是,评估task.Result将阻塞,直到完成查询。

最简单的方法可能是让Search方法进行_dispatcher.BeginInvoke在结束通话代替。

另一种选择 - 这将成为C#5更容易 - 将延续添加到任务,这样,当它完成后,您可以更新UI。 此刻的你会使用Task.ContinueWith ; 用C#5你会使用asyncawait



文章来源: GUI froze while Dispatcher.BeginInvoke or Task.StartNew